【发布时间】:2009-04-13 11:19:40
【问题描述】:
对于表格视图单元格中的每个条目,我需要在右侧有一个按钮,然后是一些文本,然后在左侧再有一个按钮。在按钮单击事件中,我需要更改文本(单击左/右按钮。)并根据文本条件删除任一按钮。我无法使用 cellForRowAtIndexPath 方法删除按钮。我尝试子类化 UITableViewCell 并使用方法 -prepareForReuse 但我无法重置单元格。任何想法我如何实现这一目标?或者有什么方法可以让这个按钮不可见或隐藏?
NSString *CellIdentifier = [[NSString alloc] initWithFormat:@"Cell%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[self.tableView clearsContextBeforeDrawing];
NSString *str = [listOfItems objectAtIndex:indexPath.row];
cell.text = [listOfItems objectAtIndex:indexPath.row];
cell.textColor = [UIColor blueColor];
cell.font = [UIFont systemFontOfSize:14];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *image = [UIImage imageNamed:@"Arrow_Right.png"];
CGRect frame = CGRectMake(290, 5, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:@selector(rightArrow_clicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *img = [UIImage imageNamed:@"Arrow_Left.png"];
CGRect frame1 = CGRectMake(5, 5, img.size.width, img.size.height);
button1.frame = frame1;
[button1 setBackgroundImage:img forState:UIControlStateNormal];
button1.backgroundColor = [UIColor clearColor];
[button1 addTarget:self action:@selector(leftArrow_clicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button1];
if([str isEqual:@" abc "])
[button setEnabled:NO];
if([str isEqual:@" pqr "])
[button1 setEnabled:NO];
【问题讨论】:
-
是的,它是一个可重复使用的单元格。如何在 -prepareForReuse 方法中重置它?
标签: iphone objective-c cocoa cocoa-touch