Add last cell to UItableview

I have a UITableView whose data source is NSMutableArray . An array consists of a set of objects. All cells are displayed in the correct order.

Now I want to know how to display the last cell always with some text that is not in the data array.

Hope I'm 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 an NSMutableArray exception from the limits.

What could be wrong?

+3
source share
3 answers

- , . list , 0, - 1. , count, count + 1. , count - else. count 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];
}
+1
    - (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;
}
+5

in numberOfRowInSectionreturn number of rows = your array count +1, then in the cell for cellForRowAtIndexPathcheck for indexPath.row if it is an equla for your array count +1, then create the cell that you want to add at last.

+1
source

All Articles