0

我为我的表格创建了自定义单元格。基本上我的单元格上有 UIButton 。我正在重用这些细胞。

但是我对这些按钮有疑问,因为当重用单元格时,它的所有元素也会被重用,但我正在寻求这些按钮对于每个单元格都是唯一的。

也许有人可以为此功能提出解决方案?

4

2 回答 2

0

如果您button的自定义单元格上有一个属性,该怎么办?

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath    static NSString *CellIdentifier = @"My Cell Identifier";
        MyCustomCellClass *cell = (MyCustomCellClass *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[MyCustomCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        // Configure the cell

        UIButton *myButtonForThisCell = [MyButtonArray objectAtIndex:indexPath.row];

        cell.button = myButtonForThisCell;
        return cell;
    }               
于 2012-10-23T06:25:53.583 回答
0

使用两种方法创建一个 buttonView UIVIEW 文件

-(NSInteger) getGridIndex
{
    return gridIndex;
}

-(void) setGridIndex:(NSInteger)value
{ 
    gridIndex = value;
}

现在在 cellforrowatindexpath 方法中

        if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                cell.accessoryType = UITableViewCellAccessoryNone;

        ButtonView *buttonView=[[ButtonView alloc] initWithFrame:CGRectMake(110, 110, 160, 26)];
                buttonView.tag=18;
         UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(55, 0, 60, 26)];
[button addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
         [buttonView addSubview:button];
          [cell addSubview:buttonView];
        }
     ButtonView *buttonView=(ButtonView*)[cell viewWithTag:18];
     NSArray *d=[buttonView subviews];
      UIButton *button=[d objectAtIndex:0];
    //here you can change the setting or title of button or whatever u want to do.
      [buttonView setGridIndex:indexPath.row];
    }

现在在按钮操作中:

-(void)ButtonAction:(UIButton*)sender{
    ButtonView *view = (ButtonView *)[sender superview];
    NSInteger n=[view getGridIndex];
    //here n is the indexpath.row at which the button was tapped..you can write its action accordingly.
}
于 2012-10-23T06:26:00.993 回答