她tableAllconacts是我的NSMutableArray無法更改按鈕的背景圖像中的UITableView如果一旦設定
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle= UITableViewCellSeparatorStyleNone;
btnChk =[UIButton buttonWithType:UIButtonTypeCustom];
btnChk.frame =CGRectMake(280, 4, 32, 32);
btnChk.backgroundColor =[UIColor clearColor];
[btnChk addTarget:self action:@selector(btnCheckmarkClicked:) forControlEvents:UIControlEventTouchUpInside];
btnChk.tag=indexPath.row;
UIImageView *separator=[[UIImageView alloc ]initWithFrame:CGRectMake(0, 44, 320, 1)];
separator.image=[UIImage imageNamed:@"divider.png"];
[cell.contentView addSubview:separator];
}
cell.textLabel.font = [UIFont systemFontOfSize:18];
cell.textLabel.textColor =[UIColor whiteColor];
cell.textLabel.text = [appdeleObj.arrDriverName objectAtIndex:indexPath.row];
NSString *strValue=[appdeleObj.arrDriverName objectAtIndex:indexPath.row];
NSLog(@"%@",strValue);
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"Checked"]isEqualToString:strValue])
{
if ([[NSUserDefaults standardUserDefaults ] boolForKey:@"showChkmark"])
{
[btnChk setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal];
}
else
{
[btnChk setBackgroundImage:[UIImage imageNamed:@"radio_off.png"] forState:UIControlStateNormal];
}
}
else
{
NSLog(@"%@",btnChk.currentBackgroundImage);
[btnChk setBackgroundImage:[UIImage imageNamed:@"radio_off.png"] forState:UIControlStateNormal];
}
[cell.contentView addSubview:btnChk];
return cell;
}
按鈕操作的代碼更改背景圖片
-(void)btnCheckmarkClicked: (UIButton *)sender
{
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tableAllContacts];
NSIndexPath *indexPath = [tableAllContacts indexPathForRowAtPoint:touchPoint];
// No need to use tag sender will keep the reference of clicked button
UIButton *button = (UIButton *)sender;
appdeleObj.strCheckUnchek =[appdeleObj.arrDriverName objectAtIndex:indexPath.row];
if ([button.currentBackgroundImage isEqual:[UIImage imageNamed:@"radio_on.png"]])
{
[[NSUserDefaults standardUserDefaults] setValue:@"radio_off.png" forKey:@"Checked"];
// [button setBackgroundImage:nil forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"showChkmark"];
}
else
{
[[NSUserDefaults standardUserDefaults] setValue:appdeleObj.strCheckUnchek forKey:@"Checked"];
// [button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showChkmark"];
}
[tableAllContacts reloadData];
}
首先檢查它正在改變的NSUserDefaults值或ont –
nsuserdefaults中的值完全改變,但當上述所有檢查工作正常時,設置的背景圖像未更新 –
ok然後刪除單元格的狀態= =無] –