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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:04:48  来源:igfitidea点击:

Add a last cell to UItableview

iphonexcodeuitableview

提问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 listhas count objects indexed 0 to count - 1. So you must check against countand not count + 1. As it is now, the request for countth row is entering the elsesection. There is no object at countin the listarray. So you are getting the error. This is the modification.

您的问题是您正在检查索引的值。在list具有计数的对象索引0计数- 1,所以,你必须核对count并没有count + 1。就像现在一样,count第 th 行的请求正在进入该else部分。没有在对象countlist阵列。所以你得到了错误。这是修改。

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 numberOfRowInSectionreturn number of rows = your array count +1then in Cell for cellForRowAtIndexPathcheck for indexPath.row if it is equla to your array count +1then create cell that you want to add at last.

作为numberOfRowInSection回报,number of rows = your array count +1然后在 Cell 中cellForRowAtIndexPath检查 indexPath.row 如果它与您的数组相等,count +1然后创建您最后要添加的单元格。