【发布时间】:2015-01-31 06:45:03
【问题描述】:
我正在创建一个应用来显示列表,每个列表都有 x 个要显示在表格单元格中的图像。
要显示图像,我必须动态创建 UIImageView 并在 for 循环中将图像加载到单元格中{取决于从服务器调用接收到的数据}。
现在,我可以动态添加图像,但单元格中显示的图像数量不正确。我检查了循环的数组值和计数,但仍然无法理解为什么图像无法正确加载。
问题: 加载视图时加载的前 3-4 个单元显示正确数量的以编程方式添加的图像视图。但是当我滚动到表格底部时,单元格中不会显示正确数量的图像并显示随机数量的图像
这里是功能代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cellidentifier1 = @"VenueCell";
VenueCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
// Configure the cell...
long row = [indexPath row];
if (!cell.hasImageViews) {
cell.hasImageViews = YES;
NSArray *individualSports = [_venueSports[row] componentsSeparatedByString:@"|"] ;
NSLog(@"sports132431 = %@",individualSports);
for (int t = 0; t<individualSports.count; t++) {
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake((250/10*t+10), 125, 20, 20)];
if ([individualSports[t] isEqual:@"Cricket"]) {
[imageView setImage:[UIImage imageNamed:@"cricket_unselected.png"]];
} else if ([individualSports[t] isEqual:@"Basketball"]) {
[imageView setImage:[UIImage imageNamed:@"basketball_unselected.png"]];
} else if ([individualSports[t] isEqual:@"Football"]) {
[imageView setImage:[UIImage imageNamed:@"football_unselected.png"]];
} else if ([individualSports[t] isEqual:@"Lawn Tennis"]) {
[imageView setImage:[UIImage imageNamed:@"lawn_unselected.png"]];
} else if ([individualSports[t] isEqual:@"Squash"]) {
[imageView setImage:[UIImage imageNamed:@"squash_unselected.png"]];
} else if ([individualSports[t] isEqual:@"Table Tennis"]) {
[imageView setImage:[UIImage imageNamed:@"table_unselected.png"]];
} else if ([individualSports[t] isEqual:@"Badminton"]) {
[imageView setImage:[UIImage imageNamed:@"badminton_unselected.png"]];
} else if ([individualSports[t] isEqual:@"Pool"]) {
[imageView setImage:[UIImage imageNamed:@"pool_unselected.png"]];
} else if ([individualSports[t] isEqual:@"Swimming"]) {
[imageView setImage:[UIImage imageNamed:@"swiming_unselected.png"]];
}
[cell addSubview:imageView];
}
individualSports = [[NSArray alloc] init];
NSLog(@"sports = %@",_venueSports[row]);
NSLog(@"asx=%@",individualSports);
}
return cell;
}
// hasImageViews是VenueCell类的bool变量
任何建议都是好的。 提前致谢!
更新:这是更新后的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
long row = [indexPath row];
// Configure the cell...
long row = [indexPath row];
cell.vName.text = _venueName[row];
cell.vTiming.text = _venueTiming[row];
cell.vAreaName.text = _venueArea[row];
cell.starRatingImage.backgroundImage=[UIImage imageNamed:@"starsbackground iOS.png"];
cell.starRatingImage.starImage = [UIImage imageNamed:@"star.png"];
cell.starRatingImage.starHighlightedImage = [UIImage imageNamed:@"starhighlighted.png"];
cell.starRatingImage.maxRating = 5.0;
cell.starRatingImage.delegate = self;
cell.starRatingImage.horizontalMargin = 12;
cell.starRatingImage.editable=NO;
cell.starRatingImage.rating=[_venueAvgRating[row] floatValue] ;
cell.starRatingImage.displayMode=EDStarRatingDisplayAccurate;
NSArray *individualSports = [_venueSports[row] componentsSeparatedByString:@"|"] ;
for (int t = 0; t<individualSports.count; t++) {
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake((25*t+10), 125, 20, 20)];
// Since each of your individualSports[t] options
// first words is simply the prefix to your image file name,
// you can simplify the contents of your loop like so:
NSString *firstWord = (NSString*)[[individualSports[t] componentsSeparatedByString:@" "] objectAtIndex:0];
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_unselected.png", firstWord.lowercaseString]]];
[cell addSubview:imageView];
}
if([_venueCategory[row] isEqualToString:@"Partner"]){
cell.vImageCategory.image=[UIImage imageNamed:@"venue_partner.png"];
}
else if([_venueCategory[row] isEqualToString:@"Verified"]){
cell.vImageCategory.image=[UIImage imageNamed:@"venue_verified.png"];
}
else{
cell.vImageCategory.image=[UIImage imageNamed:@"venue_unverified.png"];
}
cell.vImage.image=[UIImage imageNamed:@"agon_logo.png"];
return cell;
}
【问题讨论】:
-
还没有真正查看过你的代码,但是这个等式:250/10*t+10 等于 25*t+10,所以你可以大大简化它
-
而且由于您正在重用您的单元格,因此此比较结果
if (!cell.hasImageViews) {可能没有达到您的预期,因为您也在重用该属性。此外,在这种情况下,您在重用单元格的同时添加子视图是一个很大的禁忌。 -
我不认为你应该使用循环来分配图像。你应该将运动类型的标题存储在一个数组中。并在 cell.textLabe.text=[sportsArray objectAtIndex:indexpath.Row 中使用它];
-
我必须添加图像而不是标签和数字,所以图像不知道所以我必须添加 for 循环
-
@Lyndsey Scott:我是 ios 开发的新手,请您指导我找到解决此问题的正确方法
标签: ios objective-c image uitableview