【问题标题】:UITableViewCell not getting updatedUITableViewCell 没有得到更新
【发布时间】: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


【解决方案1】:

您要做的是子类化 UITableViewCell,并在 init 方法中添加子视图,以便它们只添加一次。

最有可能发生的情况是您正在将子视图添加到可重复使用的单元格中,该单元格上很可能已经有以前使用过的按钮。此外,您可以将按钮设置为“不可见”,如下所示:

[button setHidden:YES];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 2021-04-10
    相关资源
    最近更新 更多