gpt4 book ai didi

ios - 在表格 View 单元格中显示图像

转载 作者:行者123 更新时间:2023-11-28 21:09:58 25 4
gpt4 key购买 nike

我必须在单击图像上的前进按钮时在 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

最佳答案

您当前的代码没有使用适当的关注点分离;您的 TableView Controller 不关心单元格中当前显示的是哪个图像,因此尝试处理 TableView Controller 中的按钮点击是不必要的复杂。

您没有显示所有源代码,所以我所拥有的可能与您的代码 100% 不匹配——我只是在做有根据的猜测。此外,您似乎有两个不同的图像阵列,我认为这只是您的测试方法。实际上,每个单元格应该有一个图像数组,每个数组都包含在一些其他数据结构中,其中一个数组是您的 tableview 的数据源。

LotsandLandTableCell.h

-(void)setImages:(NSArray *)images;

LotsandLandTableCell.m

 @property (weak, nonatomic) NSArray *bigImages;
@property NSInteger imageIndex;

-(void) prepareForReuse {
self.bigImages = nil;
self.bigimage.image = nil;
self.imageIndex = 0;
}

-(void) setImages:(NSArray *)images {
if (images != null) {
self.bigImages = images;
self.imageIndex = 0;
[self updateBigImage];
}
}

@IBAction forwardTapped:(UIButton *)sender {
if (self.images.count == 0) {
return;
}
self.imageIndex = (self.imageIndex + 1) % self.images.count;
[self updateBigImage];
}

@IBAction backwardTapped:(UIButton *)sender {
if (self.images.count == 0) {
return;
}
self.imageIndex -= 1;
if (self.imageIndex < 0) {
self.imageIndex = self.bigImages.count;
}
[self updateBigImage];
}

- (void) updateBigImage {
self.bigimage.image = [UIImage imageNamed:self.images[self.imageIndex]];
}

View Controller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
LotsandLandTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[LotsandLandTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}

NSArray *yourImagesArray = yourDataArray[indexPath.row].bigImages; // Change this

[cell setImages:yourImageArray];

return cell;
}

关于ios - 在表格 View 单元格中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43949904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com