0

私の最近の欲求不満は、それぞれの特定の条項に従って、それぞれのUIButtonサブビューです。私のコードはほとんど次のとおりです。UITableViewCellUITableViewsetHidden: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;
}

また、initメソッドとupdateメソッドは次のとおりです。

- (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句に従って期待どおりに表示されないことです。どんな助けでも大歓迎です。前もって感謝します!

4

2 に答える 2

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
}

これで十分です

于 2012-12-19T12:35:20.540 に答える
0

このコードをCellForRowAtIndexPathAlsoで使用します。

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

    if(object.meetsCondition) {
        btnMy.hidden = NO;
    }
    else {
        btnMy.hidden = YES;
    }
于 2012-12-19T11:38:00.020 に答える