gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:18:41 26 4
gpt4 key购买 nike

我有一个支持 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com