开发者

Add a last cell to UItableview

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.

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, 开发者_StackOverflowi.iQty,i.iUnit];
    }
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

but i get the NSMutableArray out of bounds exception.

What could be wrong ?


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜