2012-10-23 57 views
0

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

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

也许有人可以提出这个功能的解决方案?

+0

要知道哪一行一按钮处于按下状态时:http:// stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number/9274863#9274863 – jrturton

+0

@jrturton:该调试器变量完成弹出不断跟踪我!我们需要找出如何做到这一点。 – NSTJ

回答

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

在按钮动作创造的cellForRowAtIndexPath方法有两种方法buttonView UIView的文件

-(NSInteger) getGridIndex 
{ 
    return gridIndex; 
} 

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

现在现在

 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. 
} 
相关问题