xcode 使用 indexPath.row 从数组中获取对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10457649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:11:44  来源:igfitidea点击:

Using indexPath.row to get an object from an array

objective-cxcodeuitableview

提问by ebsp

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

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

- (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];
}

Error message:

错误信息:

-[__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'

回答by Titouan de Bailleul

Why don't you just use

你为什么不直接使用

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

?

?

回答by onegray

In addToBasketmethod you put NSNumberobject into basketarray, but in cellForRowAtIndexPathmethod you expect NSStringobjects in basket. To make your code working you can use safe conversion to string:

addToBasket方法你把NSNumber对象变成basket阵列,但在cellForRowAtIndexPath方法你期望NSString中的对象basket。为了使您的代码正常工作,您可以使用安全转换为字符串:

NSString *name = [NSString stringWithFormat:@"%@",[basket objectAtIndex:intrNum]];

回答by Sakares

When your code

当你的代码

int intrNum = [num intValue];

the intrNummay is not real IndexPath Integer it may return the kind of address (example like '88792' or etc.) . So, It will cause problem with your array

intrNum5月是不是真正的IndexPath整数,可以返还类型的地址(例如像“88792”或等)。所以,它会导致你的阵列出现问题

Correct your get object from array code in cellForRowAtIndexPathlike this

cellForRowAtIndexPath像这样从数组代码中更正您的 get 对象

- (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:indexPath.row];


cell.textLabel.text = name;
return cell;

}

}

and in the addToBasketyou should not use numberWithIntlike the reason about intthat I've mentioned above.

并且在addToBasket您不应该使用我上面提到numberWithInt的原因int

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

    // [basket insertObject:[NSNumber numberWithInt:prodID] atIndex:0];
    [basket insertObject:[NSNumber numberWithInteger:prodID] atIndex:0];    

    [self.tableView reloadData];
}

Hope it helps you!

希望对你有帮助!