将最后一个单元格添加到UItableview [英] Add a last cell to UItableview

查看:119
本文介绍了将最后一个单元格添加到UItableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableView whose datasource is a NSMutableArray. The array consists of a set of objects. All the cells are displayed in proper order.

现在我想知道如何显示最后一个单元格,只有一些文本,而数据源数组中没有。

Now I want to know how to display a last cell always with some text alone which is not there in the datasource array.

我希望我足够清楚:)

编辑:------- ---

-(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超出范围的例外。

but i get the NSMutableArray out of bounds exception.

可能出现什么问题?

推荐答案

你的问题是价值你正在检查索引。 列表将计数对象编入索引为0 - 1.因此,您必须检查 count 而不是 count + 1 。就像现在一样, count 行的请求正在输入 else 部分。 列表数组中的 count 没有任何对象。所以你得到了错误。这是修改。

Your problem is the value you are checking against index. The list has count objects indexed 0 to count - 1. So you must check against count and not count + 1. As it is now, the request for countth row is entering the else section. There is no object at count in the list array. So you are getting the error. This is the modification.

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

这篇关于将最后一个单元格添加到UItableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆