简体   繁体   中英

How to enable/disable in each row of UITableview iOS

I need to disable/enable uibutton in each row of UItableview, I compare 2 NSMutablearray to enable/disable it.I tried below code but it always enable button in first row. Where's mistake in my code?

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

Thanks for your help.

Declare your UIButton before checking if cell is nil and creating it. Create UIButton only if cell is nill If cell already exists, get it by tag.

UIButton *pausebtn;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM