xcode 将最后一个单元格添加到 UItableview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6068144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a last cell to UItableview
提问by Neelesh
I have a UITableViewwhose datasource is a NSMutableArray. The array consists of a set of objects. All the cells are displayed in proper order.
我有一个UITableView ,其数据源是NSMutableArray。该数组由一组对象组成。所有单元格都以正确的顺序显示。
Now I want to know how to display a last cell always with some text alone which is not there in the datasource array.
现在我想知道如何显示最后一个单元格,并且总是单独显示一些文本,而这些文本不在数据源数组中。
I hope i am clear enough :)
我希望我足够清楚:)
EDIT :----------
编辑 : - - - - -
-(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;
}
but i get the NSMutableArray out of bounds exception.
但我得到 NSMutableArray 越界异常。
What could be wrong ?
有什么问题?
采纳答案by Deepak Danduprolu
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 count
th 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.
您的问题是您正在检查索引的值。在list
具有计数的对象索引0计数- 1,所以,你必须核对count
并没有count + 1
。就像现在一样,count
第 th 行的请求正在进入该else
部分。没有在对象count
中list
阵列。所以你得到了错误。这是修改。
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];
}
回答by Chetan Bhalara
- (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;
}
回答by saadnib
in numberOfRowInSection
return number of rows = your array count +1
then in Cell for cellForRowAtIndexPath
check for indexPath.row if it is equla to your array count +1
then create cell that you want to add at last.
作为numberOfRowInSection
回报,number of rows = your array count +1
然后在 Cell 中cellForRowAtIndexPath
检查 indexPath.row 如果它与您的数组相等,count +1
然后创建您最后要添加的单元格。