简体   繁体   中英

How to add a button to ios TableView row

I have a text array backing a TableView in iOS. In the cellForRowAtIndexPath : method I return a UITableViewCell* which is populated with text from the backing array. indexPath is used as the index into the backing array.

I now want to add a "Done" button to the last cell in the TableView . In my StoryBoard I've created a second (prototype) TableView Cell and gave it the identifier "ButtonCell". I've also added an extra element to the end of the backing array so numberOfRowsInSection: can return the count of the backing array and everything will just work.

I thought I would set the text of the last array element to something like @"donebutton" and then I could check for that in cellForRowAtIndexPath:. If it comes up true, I would know I'm at the end of my array and to return the "ButtonCell" cell instead of the normal "Cell". Thing is, it's not quite working right. What's the best way to accomplish this? Code snip is below.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

   static NSString *ButtonCellIdentifier = @"ButtonCell";
   UITableViewCell *bcell = [tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier forIndexPath:indexPath];

   NSString *rowtext = [_mArCellData objectAtIndex:indexPath.row];

   // return button cell if last item in list
   if ([rowtext isEqualToString:[NSString stringWithFormat:@"%d", SUBMIT_BUTTON]])
   {
      NSLog(@"hit last row, so using button row");
      return bcell;
   }

   cell.textLabel.text = rowtext;
   return cell;
}

这就是我得到的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    static NSString *ButtonCellIdentifier = @"ButtonCell";

    UITableViewCell *cell;

    if (indexPath.row != ([_mArCellData count] - 1) { // if not the last row

        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        // configure cell...

    } else { // last row

        cell = [tableView dequeueReusableCellWithIdentifier:ButtonCell];
        // configure button cell...
    }

    return cell;
}

I would just change your if statement to:

if ([tableView numberOfRowsInSection:0] == indexPath.row + 1) {
   NSLog(@"hit last row, so using button row");
   bcell.textLabel.text = rowtext;
   return bcell;
}

This is a little more abstracted than your solution and doesn't rely on a property of a cell being set to anything in particular. I like the way @bilobatum put the dequeueReusableCellWithIdentifier: call in the if statement. That should save some memory as well.

EDIT: I also noticed that you are setting cell text, but not bcell text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM