使用 indexPath.row 从数组中获取对象 [英] Using indexPath.row to get an object from an array

查看:36
本文介绍了使用 indexPath.row 从数组中获取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 indexPath.row 从数组中获取对象?我已尝试使用以下代码,但它返回信号 SIGABRT".. 请帮忙

How do i use indexPath.row to get an object from an array? I have tried with the following code but it returns "signal SIGABRT".. Please help

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *inde = [NSString stringWithFormat:@"%d", indexPath.row];
    NSNumber *num = [NSNumber numberWithInteger: [inde integerValue]];
    int intrNum = [num intValue];
    NSString *name = [basket objectAtIndex:intrNum];


    cell.textLabel.text = name;
    return cell;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    basket = [[NSMutableArray alloc] init];
    [basket addObject:@"1"];
    [self makeGrid];
 }


- (void)addToBasket:(id)sender {
    NSInteger prodID = ((UIControl*)sender).tag;

    [basket insertObject:[NSNumber numberWithInt:prodID] atIndex:0];
    [self.tableView reloadData];
}

错误信息:

-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6866080 2012-05-05 02:29:18.208 app[7634:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6866080'

推荐答案

为什么不直接使用

NSString *name = [basket objectAtIndex:indexPath.row];

?

这篇关于使用 indexPath.row 从数组中获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆