如何在UITableview iOS的每一行中启用/禁用

时间:2013-11-29 12:27:43

标签: ios objective-c uitableview

我需要在UItableview的每一行中禁用/启用uibutton,我比较2 NSMutablearray来启用/禁用它。我尝试了下面的代码,但它始终在第一行启用按钮。我的代码中哪里出错了?

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row];

    UITableViewCell *cell= [_tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    UIButton *pausebtn= [UIButton buttonWithType:UIButtonTypeCustom];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];
        [pausebtn setFrame:CGRectMake(285, 10, 30,30)];
        [pausebtn setTag:4000];
        [pausebtn setImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];
        [pausebtn addTarget:self action:@selector(pausebtnClicked:event:) forControlEvents:UIControlEventTouchDown];
        [cell.contentView addSubview:pausebtn];

    }

     pausebtn = (UIButton*)[cell.contentView viewWithTag:4000];
    [pausebtn addTarget:self action:@selector(pausebtnClicked:event:) forControlEvents:UIControlEventTouchDown];
    [pausebtn setImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];

   //check 2 arrays

   if ([[Array1 objectAtIndex:indexPath.row] integerValue] != [[Array2 objectAtIndex:indexPath.row] integerValue]) 
   {
        pausebtn.enabled = YES;
   }
   else{
       pausebtn.enabled = NO;
   }


   return cell;
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在检查单元格是否为零并创建它之前声明您的UIButton。 仅当单元格为nill时才创建UIButton 如果单元格已存在,请通过标记获取。

UIButton *pausebtn;

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];
    pausebtn= [UIButton buttonWithType:UIButtonTypeCustom];
    .....

}
    pausebtn = (UIButton*)[cell.contentView viewWithTag:4000];
    .....