objective-c - UITableViewCell 中的 UIButton subview 未按预期隐藏

标签 objective-c ios xcode

我最近的挫败感是我的 UITableView 的每个 UITableViewCell 中有一个 UIButton subview ,我想setHidden:根据每个 indexPath 的特定子句。我的代码大致如下:

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [self initCell:cell forIndexPath:indexPath];
    }
    [self updateCell:cell forIndexPath:indexPath];
    return cell;
}

初始化和更新方法如下:

- (void)initCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
    ...

    UIButton *btnMy = [UIButton buttonWithType:UIButtonTypeCustom];
    btnMy.tag = kButtonMyTag;
    [btnMy setFrame:CGRectMake(170, 45, 100, 30)];
    [btnMy setBackgroundImage:[UIImage imageNamed:@"btn_image"] forState:UIControlStateNormal];
    btnMy.adjustsImageWhenHighlighted = YES;
    [btnMy setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btnMy.titleLabel.font = [UIFont fontWithName:@"MyFont" size:14];
    [btnMy addTarget:self action:@selector(btnMyPressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:btnMy];

    UIImageView *imgViewAccessory = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"table_accessory"]];
    cell.accessoryView = imgViewAccessory;
    [imgViewAccessory release];
}

- (void)updateCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
    UIButton *btnMy = (UIButton *)[cell viewWithTag:kButtonMyTag];

    MyObject *object = (MyObject *)[self.dataSource objectAtIndex:indexPath.row];

    if(object.meetsCondition) 
    {
        btnMy.hidden = NO;
    }
    else 
    {
        btnMy.hidden = YES;
    }
    ...
}

令人沮丧的结果是,滚动时按钮随机显示和隐藏,而不是按照 updateCell 方法中的 if 子句所预期的那样。 任何帮助将非常感激。提前致谢!

最佳答案

您应该制作自定义单元格,并根据条件显示和隐藏按钮

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *nib;
    static NSString *cellIdentifier= @"cell";

    UITableViewCell *theCell = [self.tblView dequeueReusableCellWithIdentifier:cellIdentifier];

    if([theCell.contentView subviews]){
        for(UIView *view in [theCell.contentView subviews]){
            [view removeFromSuperview];
        }
    }

    if(theCell== nil)
    {
        nib  = [[NSBundle mainBundle] loadNibNamed:@"Your custom cell name" owner:self options:nil]; 
        theCell = [nib objectAtIndex:0];
        theCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    UIButton *btn=(UIButton*)[theCell.contentView viewWithTag:101];
if(yourcondition)
//hide button
else
//show button
}

这样就可以了

关于objective-c - UITableViewCell 中的 UIButton subview 未按预期隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13951439/

相关文章:

ios - 推送 View Controller 的背景颜色

ios - if 语句创建已知汇率的货币

iOS plist.info : where should I put additional keys with a nested dict tag?

iphone - 如何使用默认请求地址编写UIWebView,例如(192.168.1.1)

c# - 从 Windows 上的 C# 迁移到 Mac 上的 Objective-C

c++ - 在 Mac OS X 上安装 OpenCV 3 作为框架

objective-c - 用于编写 Objective-C 代码的良好 xcode 替代方案

c++ - OpenGL 不会绘制三角形,遵循精确的格式

在 Xcode 中运行的 C++ 程序,但不在 Eclipse 中

ios - 翻转 UICollectionViewCell 会导致其他单元格翻转