UITable视图工作正常。滚动时,选中的图像更改为取消选中。而且我还需要在SAVE按钮Action中获取特定的已检查图像数据。任何人体都可以帮我解决这个问题。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle= UITableViewCellSelectionStyleNone;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
[btn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal]; [cell addSubview:btn];
btn.tag=4;
forState:UIControlStateNormal];
[btn addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
lbl_name.text=[NSString stringWithFormat:@"%@",[[arr1 valueForKey:@"Name"]objectAtIndex:indexPath.row]];
lbl_name.tag=5;
lbl_name.textColor=[UIColor blackColor];
[lbl_name setTextAlignment:NSTextAlignmentCenter];
lbl_name.font=[UIFont systemFontOfSize:15];
[cell addSubview:lbl_name];
return cell;
}
-(void)checkBoxClicked:(id)sender
{
UIButton *tappedButton = (UIButton*)sender;
if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"unchecked.png"]])
{
[sender setImage:[UIImage imageNamed: @"checked.png"] forState:UIControlStateNormal];
} else {
[sender setImage:[UIImage imageNamed:@"unchecked.png"]forState:UIControlStateNormal];
}
}
答案 0 :(得分:0)
你使用传出状态来设置这样的图像。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle= UITableViewCellSelectionStyleNone;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
[btn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed: @"checked.png"] forState:UIControlStateSelected];
btn.selected=NO;
btn.tag=4;
[btn addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
lbl_name.text=[NSString stringWithFormat:@"%@",[[arr1 valueForKey:@"Name"]objectAtIndex:indexPath.row]];
lbl_name.tag=5;
lbl_name.textColor=[UIColor blackColor];
[lbl_name setTextAlignment:NSTextAlignmentCenter];
lbl_name.font=[UIFont systemFontOfSize:15];
[cell addSubview:lbl_name];
return cell;
}
-(void)checkBoxClicked:(id)sender
{
UIButton *tappedButton = (UIButton*)sender;
if (tappedButton.isSelected) {
tappedButton.selected=NO;
}
else
{
tappedButton.selected=YES;
}
}
答案 1 :(得分:0)
取一个数组,用于存储所选按钮的IndexValue,
@property (nonatomic,retain) NSMutableArray *arySelected;
在ViewDidLoad中初始化数组,
- (void)viewDidLoad {
[super viewDidLoad];
self.arySelected = [[NSMutableArray alloc] init];
}
更改TableViewCell委托代码,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle= UITableViewCellSelectionStyleNone;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
if (![arySelected containsObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]])
{
[btn setImage:[UIImage imageNamed: @"checked.png"] forState:UIControlStateSelected];
}
else
{
[btn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}
btn.tag=indexPath.row;
[btn addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
lbl_name.text=[NSString stringWithFormat:@"%@",[[arr1 valueForKey:@"Name"]objectAtIndex:indexPath.row]];
lbl_name.tag=5;
lbl_name.textColor=[UIColor blackColor];
[lbl_name setTextAlignment:NSTextAlignmentCenter];
lbl_name.font=[UIFont systemFontOfSize:15];
[cell addSubview:lbl_name];
return cell;
}
//在你的按钮事件上,
-(void)checkBoxClicked:(id)sender
{
sender.selected = ! sender.selected;
if (sender.selected)
{
[arySelected addObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
}
else
{
[arySelected removeObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
}
[self.tableView reloadData];
}