ios - 如何将按钮添加到 ios TableView 行

标签 ios tableview

我有一个支持 TableView 的文本数组在 iOS 中。在 cellForRowAtIndexPath :方法我返回一个 UITableViewCell*,它填充了来自支持数组的文本。 indexPath 用作后备数组的索引。

我现在想在 TableView 的最后一个单元格中添加一个“完成”按钮。 .在我的 StoryBoard 中,我创建了第二个(原型(prototype))TableView Cell并给它一个标识符“ButtonCell”。我还在后备数组的末尾添加了一个额外的元素,因此 numberOfRowsInSection: 可以返回后备数组的计数,一切都会正常工作。

我想我会将最后一个数组元素的文本设置为 @"donebutton"之类的东西,然后我可以在 cellForRowAtIndexPath: 中检查它。如果它是真的,我会知道我在我的数组的末尾并返回“ButtonCell”单元而不是正常的“Cell”。问题是,它不太正常。实现这一目标的最佳方法是什么?代码片段如下。

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

This is what I'm getting

最佳答案

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

关于ios - 如何将按钮添加到 ios TableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124707/

相关文章:

jquery - iOS Safari – 如何禁用过度滚动但允许可滚动的 div 正常滚动?

iphone - 使用 Graph API 获取 Facebook 好友

ios - sectionHeader 中的自动调整字体大小

ios - Swift:如何从 TableView 中删除重复项?

快速解析查询不按升序返回对象

ios - 在 ViewController 中嵌入 UIScrollView 并且没有分页

ios - 约束 UICollectionView 高度以匹配内容的高度

ios - 如何将 NSData 转换为 C 字符串?

ios - UiTableViewCell 为表格 View 中的每个部分单选复选标记

JavaFX TableView/Column 空指针异常