[英]Add a last cell to 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超出范围的例外。
可能有什么不对?
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [your_array count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
if (row == [your_array count])
{
cell.textLabel.text = [NSString stringWithFormat:@"Some text"];
}
else
{
cell.textLabel.text = your array object text;
}
return cell;
}
在numberOfRowInSection
返回number of rows = your array count +1
然后在Cell中为cellForRowAtIndexPath
检查indexPath.row如果它与您的数组count +1
相等,则创建您想要最后添加的单元格。
您的问题是您正在检查索引的值。 该list
具有索引为0的计数对象 - 1。因此,您必须检查count
而不count + 1
。 就像现在一样,第count
行的请求正在进入else
部分。 list
数组中没有对象count
。 所以你得到了错误。 这是修改。
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.iName, i.iQty,i.iUnit];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.