这是最后一次更新。在 indexpath.section ==0 和 ==1 和 ==2 中设置文本并出现错误时崩溃

[主对象索引:]:未识别的选择器发送到实例0x109624F20 2014-07-30 12:03:24.732 TYM-APP [1704:60B * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[主对象AtIndex:]:无法识别的选择器发送到实例 0x109624f20'

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
NSLog(@"wsolna hon");
static NSString *cellIdentifier = @"HomeTableViewCell";
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

// Configure the cell...
int row= [indexPath row];


                if (indexPath.section == 0){
                     homeObject = [homeArray[0] objectAtIndex:indexPath.row];

                         NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[homeObject.News dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
                    cell.NewsDateLabel.text= homeObject.News_Date;
                    cell.NewsLabel.attributedText= attrStr;
                    NSLog(@"news: %@", attrStr);

                }


                if (indexPath.section == 1){
                    homeObject = [homeArray[1] objectAtIndex:indexPath.row];

                  NSLog(@"value of indexpath for library: %d",row);
                         NSLog(@"library: %@",homeObject.News);
                    cell.NewsLabel.text= homeObject.Library_text;
                    cell.TestimonialNameLabel.text= @"";

            }

                if (indexPath.section == 2){
                    homeObject = [homeArray[2] objectAtIndex:indexPath.row];

                         NSLog(@"news: %@",homeObject.Testimonial_Description);
                    cell.NewsLabel.text= homeObject.Library_text;
                    cell.TestimonialNameLabel.text = homeObject.Testimonial_Name;

            }


return cell;
}
有帮助吗?

解决方案

如果部分的数量是固定的,我建议您将数据存储在多维数组中。因此,它将保护您以后免受大多数逻辑错误的影响。

// Declaring array for 3 sections
homeArray = [[NSMutableArray alloc] initWithObjects:[[NSMutableArray alloc] init], [[NSMutableArray alloc] init], [[NSMutableArray alloc] init], nil];

然后在检索数据并将其放入数组期间执行必要的逻辑分离

更新: 正如@Wain所说,不建议在里面做任何请求 cellForRowAtIndexPath:, ,因此最好将下一个片段放在缓存信息的位置

if ([moduleID isEqualToString:@"77"]){
    [homeArray[0] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
} else if ([moduleID isEqualToString:@"81"]){
    [homeArray[1] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
} else if ([moduleID isEqualToString:@"78"]){
    [homeArray[2] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
}

最后,以这种方式为您的表获取必要的 homeObject

if (indexPath.section == 0) {
    homeObject = [homeArray[0] objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
    homeObject = [homeArray[1] objectAtIndex:indexPath.row];
} else if (indexPath.section == 2) {
    homeObject = [homeArray[2] objectAtIndex:indexPath.row];
}

其他提示

这是一个单元格重用问题。

要修复,请确保每次从CellForrowAtIndexpath返回一个单元格:您始终为所有标签设置文本标签内容。这意味着明确地设置一些文本并将其他人设置为没有文本。

替代(和清洁)解决方案是为每个部分使用不同的自定义单元格。

还,每次要求您的单元格都不查询数据库。查询一次并缓存结果。每次询问都非常浪费,并将您的应用程序运行较慢。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top