作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个支持 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;
}
最佳答案
- (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/
我是一名优秀的程序员,十分优秀!