我有一个UITableView,末尾有一个特殊的行来插入一个新项目。这是有效的,但我希望它有绿色加号图标,而不必将表视图放入编辑模式。我怎么能这样做?
如果可能的话,我不想创建按钮或捆绑图像。有没有办法只使用标准的UITableView / UITableViewCell功能来做其中一个或两个?
答案 0 :(得分:2)
您想将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导航应用模板的所有修改代码)
答案 1 :(得分:1)
您可以将最后一个单元格实现为自定义单元格,并根据您的选择添加绿色图标。
请参阅教程以实现自定义单元格。
<强>更新强>
假设单元格是UITabelViewCell
的实例。
首先使用绿色图标创建一个按钮。
UIButton myGreenIconButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myGreenIconButton addTarget:self action:@selector(GreenIconButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
[myGreenIconButton setBackgroundImage:[UIImage imageNamed:@"greenIcon.png"] forState:UIControlStateNormal];
myGreenIconButton.tag = i;
myGreenIconButton.backgroundColor = [UIColor clearColor];
myGreenIconButton.frame = CGRectMake(5, 78, 15, 15);
现在将其添加为您上次UITabelViewCell
中的子视图。
[cell addSubview:myGreenIconButton];
实施GreenIconButtonClicked:
方法,在您添加绿色图标按钮
-(void) GreenIconButtonClicked:(id) sender
{
}
答案 2 :(得分:0)
不幸的是,我发现这样做的唯一方法是设置单元格的图像,这意味着您必须自己处理图像文件,而不是让UIKit为您加载它们。我建议使用UIKit Artwork Extractor来获取图片。