iphone – 将最后一个单元格添加到UItableview

我有一个UITableView,其数据源是NSMutableArray.该数组由一组对象组成.所有单元格都以正确的顺序显示.

现在我想知道如何显示最后一个单元格总是只有一些文本在数据源数组中不存在.

我希望我足够清楚:)

编辑:———-

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     // Return the number of rows in the section.
     //+1 to add the last extra row
     return [appDelegate.list count]+1;
}

// Customize the appearance of table view cells.
- (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];
    }

NSUInteger index=[indexPath row];

 if(index ==([appDelegate.list count]+1)) {
    cell.textLabel.text = [NSString stringWithFormat:@"extra cell"];    
    }else{
    Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%d %@)",i.iName,i.iQty,i.iUnit];
    }
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

但我得到NSMutableArray超出范围的例外.

可能有什么不对?

解决方法

您的问题是您正在检查索引的值.该列表具有索引为0的计数对象 – 1.因此,您必须检查计数而不计数1.就像现在一样,对countth行的请求正在进入else部分.列表数组中没有对象计数.所以你得到了错误.这是修改.

if(index == [appDelegate.list count] ) {
    cell.textLabel.text = [NSString stringWithFormat:@"extra cell"];    
}else{
    Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%d %@)",i.iUnit];
}

相关文章

在有效期内的苹果开发者账号(类型为个人或者公司账号)。还...
Appuploader官网--IOS ipa上传发布工具,证书制作工具跨平台...
苹果在9月13号凌晨(北京时间)发布 iOS 16,该系统的设备可...
计算机图形学--OpenGL递归实现光线追踪
Xcode 14打出来的包在低版本系统运行时会崩溃,报错信息是Li...