UITableViewCell没有得到更新
问题描述:
对于表格视图单元格中的每个条目,我需要一个按钮在右边,然后是一些文本,再按一下左边的按钮。在按钮点击事件时,我需要更改文本(点击左/右按钮),并根据文本条件删除任一按钮。我无法使用cellForRowAtIndexPath方法删除按钮。我试着继承UITableViewCell并使用方法-prepareForReuse,但我无法重置单元格。任何想法我怎么做到这一点?或者有什么办法让这个按钮不可见或可能隐藏?UITableViewCell没有得到更新
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];
答
什么你想要做的是子类的UITableViewCell,并在init
方法,以便他们只加一次添加子视图。
最可能发生的是您将子视图添加到可重复使用的单元格中,该单元格很可能已经具有上一次使用的按钮。此外,您可以将按钮设置为“不可见”,如下所示:
[button setHidden:YES];
是的,它是可重复使用的单元格。如何在-prepareForReuse方法中重置它? – Neo 2009-04-13 13:06:51