gpt4 book ai didi

iphone - 如何无条件地将绿色添加图标放在插入行上

转载 作者:可可西里 更新时间:2023-11-01 04:44:48 25 4
gpt4 key购买 nike

我有一个 UITableView,在末尾有一个特殊的行来插入一个新项目。这行得通,但我希望它有绿色加号图标,而不必将表格 View 置于编辑模式。我该怎么做?

如果可能的话,我不希望创建按钮或捆绑图像。有什么方法可以仅使用标准的 UITableView/UITableViewCell 功能来完成其中一项或两项工作吗?

最佳答案

您想将 accessoryView 设置为单元格:

@interface RootViewController : UITableViewController {
NSInteger nextValue;
NSMutableArray *timeIntervals;
}


@implementation RootViewController


- (NSNumber *)nextValue {
NSNumber *n = [NSNumber numberWithInteger:nextValue];
nextValue++;
return n;
}

- (void)viewDidLoad {
[super viewDidLoad];
nextValue = 1;
timeIntervals = [[NSMutableArray alloc] init];
[timeIntervals addObject:[self nextValue]];
}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [timeIntervals count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"TimeIntervalCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
UIButton *b = [UIButton buttonWithType:UIButtonTypeContactAdd];
[b addTarget:self action:@selector(addTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = b;
}

NSNumber *number = [timeIntervals objectAtIndex:indexPath.row];
cell.accessoryView.tag = indexPath.row;
cell.textLabel.text = [number stringValue];
cell.detailTextLabel.text = @"detail about this number";
return cell;
}

- (void)addTapped:(UIButton *)sender {
id cell = sender;
while (cell != nil && [cell isKindOfClass:[UITableViewCell class]] == NO)
cell = [cell superview];

if (cell == nil) {
NSLog(@"[%@ %@] sender was not in a cell",
NSStringFromClass([self class]), NSStringFromSelector(_cmd));
return;
}

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSInteger index = indexPath.row + 1; // insert after current cell
[timeIntervals insertObject:[self nextValue] atIndex:index];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"[%@ %@] not implemented", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}


@end

(这是对 Xcode 4.0.2 导航应用程序模板的所有修改代码)

关于iphone - 如何无条件地将绿色添加图标放在插入行上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6449770/

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