Ios 通过表单元格中的循环以编程方式添加图像+;网间网操作系统

Ios 通过表单元格中的循环以编程方式添加图像+;网间网操作系统,ios,objective-c,image,uitableview,Ios,Objective C,Image,Uitableview,我正在创建一个应用程序来显示列表,每个列表都有x个要显示在表格单元格中的图像 要显示图像,我必须动态创建UIImageView,并将图像加载到for循环中的单元格中{取决于从服务器调用接收的数据} 现在,我可以动态添加图像,但单元格中显示的图像数量不正确。我已经检查了数组值和循环计数,但仍然无法理解为什么图像加载不正确 问题: 加载视图时加载的第一个3-4单元格显示以编程方式添加的图像视图的正确数量。但当我滚动到表格底部时,单元格中不会显示正确数量的图像,而是显示随机数量的图像 以下是功能代码:

我正在创建一个应用程序来显示列表,每个列表都有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;

}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*Cellidentifier1=@“VenueCell”;
VenueCell*cell=[tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
//配置单元格。。。
长行=[indexPath行];
如果(!cell.hasImageViews){
cell.hasImageViews=是;
NSArray*individualSports=[[u-venueSports[row]由字符串分隔的组件:@“|”];
NSLog(@“sports132431=%@),个人体育);

对于(int t=0;t由于要重用单元格,在开始向单元格中添加新图像之前,需要清除先前添加到单元格中的图像。如果要显示的单元格不多,则可能根本不想重用单元格。

首先,您必须在单独的线程上从后台下载它们(不要在cellForAtIndexPath(可能在viewdidload中)上编写此代码,成功下载后,您必须重新加载tableview。只需使用一个自定义单元格和一个imageview即可

     -(void)viewDidLoad
    {
       UIImage *image;
        NSMutableArray *imageArray;

        for (int i=0; i<[totalImgesURLSArray count]; i++) {
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[totalImgesURLSArrayobjectAtIndex:i]]];
            image = [[UIImage alloc] initWithData:imageData];

            [imageArray addObject:image];
        }
        dispatch_async(dispatch_get_main_queue(), ^{

            [tableview reloadData];

        });
    });

}

       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {

        return [imageArray count];
    }
-(void)viewDidLoad
{
UIImage*图像;
NSMutableArray*imageArray;

对于(int i=0;i,例如数组

NSArray *sportsTypeArray=[NSArray arrayWithObjects:@"Cricket",@"Football",@"Hockey",@"Skitting",@"Running", nil];
incellforrowatinex方法

cell.textLabel.text=[sportsArray objectAtindex=indexPath.row];

if ([cell.textLabel.text isEqualToString: @"Cricket"]
{

cell.imageView.image = [UIImage imageNamed:@"cricket.png"];
}

else if ([cell.textLabel.text isEqualToString: @"Football"]
{

cell.imageView.image = [UIImage imageNamed:@"footBall.png"];
}

等等。使用循环不是个好主意。试试这个,如果有问题就说出来。

首先,因为您正在重用单元格,所以这个比较--
if(!cell.hasimageview){
--不一定为
cellForRowAtIndexPath
的当前单元格保留适当的值,因为被重用的单元格可能与当前单元格不同。例如,如果表视图中的4个单元格可以同时显示在屏幕上,当您滚动到原来不在表中的单元格5时,重用它意味着它可以包含单元格1的内容和属性值。因此比较完全无关

但在这种特定情况下,重用单元格毫无意义,因为每行单元格的全部内容完全不同。此外,由于自定义表视图单元格似乎只包含
.hasImageViews
属性,因此在这种情况下不需要使用自定义单元格,因为我们已经确定,我们不会重复使用电池

最后,您可以大大简化for循环中的条件,正如我在下面的代码中所做的那样

要实现这些更改,我建议执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    long row = [indexPath row];

    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];
    }

    return cell;

}

我还没有看过你的代码,但是这个等式:250/10*t+10等于25*t+10,所以你可以大大简化它,因为你在重用你的单元格,这个比较结果
if(!cell.hasImageViews){
可能没有达到您期望的效果,因为您也在重用该属性。而且,在这种情况下,在重用单元格的同时添加子视图是一个很大的禁忌。我认为您不应该使用循环来分配图像。您应该将运动类型的标题存储在数组中。并在cell.textlab.text中使用它=[sportsArray对象索引:indexath.Row];我必须添加图像,而不是标签和编号,因此图像未知,因此我必须添加loop@LyndseyScott:我在ios开发方面有点新手,所以你能指导我找到这个问题的正确解决方案吗:单元格的数量是动态的,因此我无法控制。你能更清楚地了解重用标识吗这是因为我是iOS开发的新手。基本上,当你重复使用单元格时,你有一个可见单元格的缓存。当一个单元格从屏幕顶部滚动时,完全相同的单元格显示在底部。如果你不清理单元格,你会看到刚刚从顶部滚动的相同信息,因为它是重复使用的同一个单元格。我不同意。在这种情况下,使用循环是必要的,因为OP不知道每行所需的图像数。NSArray*individualSports gettig data?来自服务器?@Logic:是的,数组是从服务器获取的。您可以显示存储在数组中的数据吗?实际上..当服务器的数据已经加载时,应该加载tableview。th恩,你根本不需要使用循环谢谢,我正在为你应用解决方案mentioned@Neelanshu虽然循环是必要的,但我会更新我的答案以大大简化您的实现…一秒钟…谢谢!申请this@Neelanshu还要注意的是,草坪网球是两个词,当文件名只是草坪…我实际上要做最后一个编辑我的代码来处理这种情况…一秒钟…谢谢!它就像一个符咒。问题解决了,我只需要配置单元格来显示其余的东西,就可以了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    long row = [indexPath row];

    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];
    }

    return cell;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *Cellidentifier1 = @"VenueCell";
    VenueCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];

    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"];
    // ... all the other starRatingImageCode ...
    cell.starRatingImage.displayMode=EDStarRatingDisplayAccurate;

    // Clear out any images previously added to the
    // cell during cellForRowAtIndexPath (images have been
    // assigned a tag of 1000 in the next for loop)
    for (UIView *subview in cell.subviews) {
        if (subview.tag == 1000) {
            [subview removeFromSuperview];
        }
    }

    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)];
        imageView.tag = 1000 //<-- Use a tag unique to these image views

        NSString *firstWord = (NSString*)[[individualSports[t] componentsSeparatedByString:@" "] objectAtIndex:0];
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_unselected.png", firstWord.lowercaseString]]];

        [cell addSubview:imageView];
    }

    // Assuming the only "else" condition is "default",
    // I've similarly simplified this code as I did with
    // your other conditional
    cell.vImageCategory.image = [NSString stringWithFormat:@"venue_%@.png", _venueCategory[row].lowercaseString];
    cell.vImage.image = [UIImage imageNamed:@"agon_logo.png"];

    return cell;
}