开发者

change button state in cellForRowAtIndexPath

开发者 https://www.devze.com 2022-12-14 21:01 出处:网络
this is driving me MAD now.. I have a UItableview. Based on an NSMutableArray, I populate it. I set up in reuseTableViewCellWithIdentifier with the following

this is driving me MAD now.. I have a UItableview. Based on an NSMutableArray, I populate it. I set up in reuseTableViewCellWithIdentifier with the following

cellRectangle = CGRectMake((ARROW_OFFSET + 5), (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, ARROW_WIDTH, LABEL_HEIGHT);
UIButton *tmpButton = [[UIButton alloc] initWithFrame:cellRectangle];
[tmpButton initWithFrame:cellRectangle];
[tmpButton setImage:[UIImage imageNamed:@"icon_edit.png"] forState:UIControlStateNormal];
[tmpButton setImage:[UIImage imageNamed:@"icon_no.png"] forState:UIControlStateDisabled];
[tmpButton addTarget:self actio开发者_开发问答n:@selector(editSelectedRow:) forControlEvents:UIControlEventTouchUpInside];  
tmpButton.tag = ARROW_TAG;
[cell.contentView addSubview: tmpButton];
[tmpButton release];

then in cellForRowAtIndexPath I have the following lines of code

UIButton *button = (UIButton *)[cell viewWithTag:ARROW_TAG];
[button setTag:indexPath.row];

if (counterHasStarted == 1) {
    NSLog(@"yes");
    button.enabled = NO;
} else {
    button.enabled = YES;
}

the button shows nicely, but for some reason when the counterHasStarted variable (which is an int is set, it doesn't change! I can change UILabels based on the above code (checking if counterHasChanged is 1 or 0).

Any ideas what's going on??


-cellForRowAtIndexPath: will only get called when the table view needs a new UITableViewCell because the user scrolled.

I guess you're changing counterHasStarted and expect the button enabled state to change? You could reload the data when you change counterHasStarted ([yourTableView reloadData]). Then the table view will call -cellForRowAtIndexPath: for all the cells that are currently visible, and you can enable or disable the buttons as needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消