iPhone – Add last cell to UITableView

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

Now I want to know how to show that the last cell always only has some text that does not exist in the data source 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 ==([appDelega te.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 exception of NSMutableArray out of range.

What could be wrong?

Your problem is that you are checking the value of the index. The list has a count object with an index of 0 – 1. So you have to check the count without counting 1. Just like now, the request for the countth row is entering the else part. There is no object count in the list array. So you got the error. This is a modification.

< /p>

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

I have a UITableView whose data source is NSMutableArray. The 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 only some text does not exist in the data source array.

p>

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.
- (UITa bleViewCell *)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 exception that NSMutableArray is out of range.

What might be wrong?

Your problem is that you are checking the value of the index. The list has a count object with index 0-1. Therefore, you have to check the count without counting 1. Just like now, the request for the countth row is entering the else part. There is no object count in the list array. So you get an error. This is a 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];
}

Leave a Comment

Your email address will not be published.