我正在创建自定义单元格包含UILabel
,,UIImageView
使用常量标签UILabel
,UIImageView
使用动态标签,表格有 11 个单元格,前 7 个单元格正确加载,当我更改 1、2 时,8、9、10、11 单元格图像视图发生变化, 3, 4, 分别在表格中的单元格,单元格中的标签也是相同的,我是使用图像来勾选表格中的复选框,UITapGestureRecognizer
用于更改表格中的图像视图,
我正在使用此代码.....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
cell.selectionStyle=UITableViewCellSelectionStyleGray;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 12, 20, 20)];
imageview.tag=n;
[cell.contentView addSubview:imageview];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tabimage:)];
imageview.userInteractionEnabled=YES;
[imageview addGestureRecognizer:tap];
imageview.image=[UIImage imageNamed:@"img1.jpeg"];
UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 2, 260,26)];
titleLabel.tag=222;
titleLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:titleLabel];
UILabel *dateLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 31, 260, 13)];
dateLabel.tag=333;
dateLabel.font=[UIFont systemFontOfSize:10];
dateLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:dateLabel];
}
UIImageView *imageview1=(UIImageView*)[cell.contentView viewWithTag:n];
if([array containsObject:[NSNumber numberWithInt:imageview1.tag]]) {
imageview1.image=[UIImage imageNamed:@"img2.jpeg"];
} else {
imageview1.image=[UIImage imageNamed:@"img1.jpeg"];
}
UILabel *titlelable=(UILabel*)[cell.contentView viewWithTag:222];
titlelable.text=[task objectAtIndex:indexPath.section];
NSLog(@"%i",indexPath.section);
UILabel *dateLabel=(UILabel*)[cell.contentView viewWithTag:333];
dateLabel.text=[date objectAtIndex:indexPath.section];
n++;
return cell;
}
- (void)tabimage:(id)sender {
UIImageView *iv=(UIImageView *)[sender view];
int i=iv.tag;
NSLog(@"------------%i",i);
if (iv.image==[UIImage imageNamed:@"img1.jpeg"]) {
iv.image= [UIImage imageNamed:@"img2.jpeg"];
[array addObject:[NSNumber numberWithInt:i]];
} else {
iv.image= [UIImage imageNamed:@"img1.jpeg"];
[array removeObject:[NSNumber numberWithInt:i]];
}
}