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

前端之家收集整理的这篇文章主要介绍了iphone – 将最后一个单元格添加到UItableview前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UITableView,其数据源是NSMutableArray.该数组由一组对象组成.所有单元格都以正确的顺序显示.

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

我希望我足够清楚:)

编辑:———-

@H_404_17@-(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部分.列表数组中没有对象计数.所以你得到了错误.这是修改.

@H_404_17@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]; }

猜你在找的Xcode相关文章