UITableViewCell 问题 indexPath.row 显示不正确 [英] UITableViewCell issue indexPath.row not displaying correctly

查看:41
本文介绍了UITableViewCell 问题 indexPath.row 显示不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个 UITableView,它有 3 个部分,每个部分有 3 行.我已经使用以下代码指定我希望某些数组中的对象显示为行内容;

I have set up a UITableView which has 3 sections in, with 3 rows in each. I ahve used the following code to specify that I want the objects from certain arrays to appear as the row content;

    recipeData = [[NSArray alloc] initWithObjects:@"Chicken & Veg Rissoto",@"Super Healthy Pizza",@"Salmon Burgers",nil];
    dessertsData = [[NSArray alloc] initWithObjects:@"Raspberry Parfaits",@"ChocNana YumYum",@"Grilled Choc Sandwich",nil];
    beveragesData = [[NSArray alloc] initWithObjects:@"Berry Smoothie",@"Banana Berry Smoothie",@"Cocoa Soy Smoothie",nil];
    [self setTitle:@"Recipe Table"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.recipeData count];
    return [self.dessertsData count];
    return [self.beveragesData count];

这一切都很好,直到今天晚上我在行中文本的左侧添加了图像.图像显示完美,但由于某种原因,行都填充了来自饮料数据的对象,它完全忽略了其他两组对象.下面是我用过的代码.我希望这就像错误地编写 if 语句一样简单.

This was all working fine, until this evening when I added in images to the left of the text in the row. The images display perfectly, but for some reason the rows are all populated with the objects from beveragesData and it totally disregards the other two sets of objects. The code is below that I have used. I am hoping it is something as simple as just writing the if statements incorrectly.

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

    if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
    }   
    if(indexPath.section == 0) 
        if(indexPath.row == 0)
            [cell.imageView setImage:[UIImage imageNamed:@"Main0.png"]];
            [cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
    if(indexPath.section == 0) 
        if(indexPath.row == 1)
            [cell.imageView setImage:[UIImage imageNamed:@"Main1.png"]];
            [cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
    if(indexPath.section == 0)
        if(indexPath.row == 2)
            [cell.imageView setImage:[UIImage imageNamed:@"Main2.png"]];
            [cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];

   if(indexPath.section == 1)
        if(indexPath.row == 0)
            [cell.imageView setImage:[UIImage imageNamed:@"Dessert0.png"]];
            [cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
    if(indexPath.section == 1)
        if(indexPath.row == 1)
            [cell.imageView setImage:[UIImage imageNamed:@"Dessert1.png"]];
            [cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
    if(indexPath.section == 1)
        if(indexPath.row == 2)
            [cell.imageView setImage:[UIImage imageNamed:@"Dessert2.png"]];
            [cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];

   if (indexPath.section == 2)
        if(indexPath.row == 0)
            [cell.imageView setImage:[UIImage imageNamed:@"Drink0.png"]];
            [cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
    if(indexPath.section == 2)
        if(indexPath.row == 1)
            [cell.imageView setImage:[UIImage imageNamed:@"Drink1.png"]];
            [cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
    if(indexPath.section == 2)
        if(indexPath.row == 2)
            [cell.imageView setImage:[UIImage imageNamed:@"Drink2.png"]];
            [cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
    return cell;

所以目前,当它运行时,UITableView 的所有 9 行都填充了来自饮料数据的对象.任何帮助将不胜感激.

So at present, when that runs all 9 rows of the UITableView are populated with the objects from beveragesData. Any help would be greatly appreciated.

谢谢

推荐答案

Objective-c 结构不是基于缩进

Objective-c structure isn't based on indentation

这个:

if(indexPath.section == 0) 
        if(indexPath.row == 0)
            [cell.imageView setImage:[UIImage imageNamed:@"Main0.png"]];
            [cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];

应该是:

if(indexPath.section == 0) 
        if(indexPath.row == 0) {
            [cell.imageView setImage:[UIImage imageNamed:@"Main0.png"]];
            [cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
        }

这就像 C/C++/Java —— if 适用于下一个语句.如果需要多个语句,则需要将语句括在花括号中.你也可以一直戴上卷发,不用担心.

It's like C/C++/Java -- the if applies to the next statement. If you need more than one, you need to enclose the statements in curly braces. You could also put in curlies all of the time to not worry about it.

还有这个代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.recipeData count];
    return [self.dessertsData count];
    return [self.beveragesData count];
}

没有做你认为它会做的事情.第一个返回总是运行,后两行被忽略(应该得到警告).

Doesn't do what you think it does. The first return is always run and the second two lines are ignored (should have gotten a warning about that).

这篇关于UITableViewCell 问题 indexPath.row 显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆