【发布时间】:2017-05-13 06:19:26
【问题描述】:
我必须在点击图像上的前进按钮时在 tableview 单元格中显示图像,但滚动后 tableview 图像正在改变...... 我已经为单元格中的每个按钮提供了标记值,并将所有单元格引用存储在数组中,然后单击前进按钮,我正在识别数组中的引用并更改该特定引用的图像..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// static NSString *cellidentifier = @"cell";
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[LotsandLandTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
// cell.smallimage.image=[UIImage imageNamed:[_smallimagesarr objectAtIndex:indexPath.row]];
//if ( [str_scroll isEqualToString: @"scrolling"]) {
// }else{
cell.bigimage.image=[UIImage imageNamed:[_bigimagesarr objectAtIndex:indexPath.row]];
//}
cell.forwardbutton.tag =indexPath.row;
NSLog(@" %p",cell);
[cell.forwardbutton addTarget:self action:@selector(buttonInfo:) forControlEvents:UIControlEventTouchUpInside];
CellModel *cellmodel=[[CellModel alloc]init];
cellmodel.tagValue=cell.forwardbutton.tag;
cellmodel.cellobj=cell;
[arrofcells addObject:cellmodel];
NSLog(@"%p",cell);
return cell;
}
-(void)buttonInfo:(UIButton *)sender{
UIButton *btn = (UIButton *)sender;
NSInteger tag = btn.tag;
CellModel *cmodel=[[CellModel alloc]init];
cmodel=[arrofcells objectAtIndex:tag];
democell=(LotsandLandTableCell *)cmodel.cellobj;
previoustag=cmodel.tagValue;
if(cmodel.tagValue==previoustag)
{
if(j<_bigimagesarr1.count){
democell.bigimage.image=[UIImage imageNamed:[_bigimagesarr1 objectAtIndex:j]];
NSLog(@"j value is is %d",j);
j++;
}else{
j=0;
[democell.forwardbutton setEnabled:NO];
}
}
}
cellModel 包含单元格引用和属性标记
@interface CellModel : NSObject
@property (nonatomic, assign) NSInteger tagValue;
@property(nonatomic,strong) id cellobj;
@end
【问题讨论】:
-
不要使用标签。为什么不简单地将图像数组提供给您的单元格并处理单元格类中的按钮?此外,每次重用单元格时,您都会添加按钮处理程序。
-
@Paulw11 没听明白,你能说得更具体些吗
-
您正在尝试处理表格视图控制器中的按钮点击,这意味着您需要确定按钮点击发生在哪个单元格中,您正在使用标签。标签很脆弱,因为单元格可以重复使用并且可以重新排序。如果您只是将图像数组设置为自定义单元格类的属性,那么每个单元格都可以处理自己的按钮点击,并且只需在其图像数组中移动。
标签: ios objective-c uitableview uibutton