她的tableAllconacts是我的nsmutablearray
- (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];
}
答案 0 :(得分:0)
更改背景图片的代码行在您发布的代码中进行了注释。
您正在方法内创建按钮。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这将绘制按钮,总是重新加载表格。我的建议是尝试在上面的方法之外创建按钮,只需在方法中设置标记。
答案 1 :(得分:0)
试试这段代码:
-(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"]])
{
//Update the BackGround Image When you click the button
[button setBackgroundImage:[UIImage imageNamed:@"radio_off.png"] forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setValue:@"radio_off.png" forKey:@"Checked"];
// [button setBackgroundImage:nil forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"showChkmark"];
}
else
{
[button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setValue:appdeleObj.strCheckUnchek forKey:@"Checked"];
// [button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showChkmark"];
}
[tableAllContacts reloadData];
}
答案 2 :(得分:0)
它可能对你有帮助。
在
if(cell == nil)
条件写下这个。
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;
[cell.contentView addSubview:btnChk]; //added this line to your code.
out if if条件使用标签
获得相同的按钮UIButton *btn2 = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
现在将您的图像应用于此按钮。
答案 3 :(得分:0)
不是这样做,为什么不为按钮的不同状态设置两个背景图像,如下所示?
[button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"]
forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:@"radio_off.png"]
forState:UIControlStateNormal];
然后你可以反转button.selected
中-(void)btnCheckmarkClicked: (UIButton *)sender
字段的值;然后该按钮将自动为您更新其backgroundImage。
button.selected = !button.selected;
希望这有帮助。