2012-12-19 52 views
0

我最近的挫折是我UITableView我根据每个indexPath具体条款要setHidden:的每一个UITableViewCell子视图UIButton。我的代码是非常以下内容:UIButton的子视图不隐藏预期

- (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; 
} 

和init和更新方法有以下几种:

- (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子句。 任何帮助将不胜感激。提前致谢!

+0

添加按钮cell.contentView代替电池.. –

+0

这没”也没有帮助。 Thanx虽然。 – ozzotto

+0

检查在这个链接的答案..http://stackoverflow.com/questions/13877137/tableviews-cells-are-changing-after-scrolling-down/ –

回答

1

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

-(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 
} 

这将做

+0

我试图避免自定义UITableViewCell实现,但这解决了我的问题。感谢阿马尔! – ozzotto

0

在您的CellForRowAtIndexPath中也使用此代码。

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

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

Thanx,但没有解决我的问题 – ozzotto

+0

更好地采取'CustomtableCell'这里是一个教程:http://www.appcoda.com/customize-table-view-cells-for-uitableview/ –