xcode UITableViewCell 问题 indexPath.row 显示不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5812261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:55:51  来源:igfitidea点击:

UITableViewCell issue indexPath.row not displaying correctly

objective-cxcodeiosuitableview

提问by Paul Morris

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;

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

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

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.

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

- (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;

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.

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

Thanks

谢谢

采纳答案by Lou Franco

Objective-c structure isn't based on indentation

Objective-c 结构不是基于缩进

This:

这个:

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

Should be:

应该:

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

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.

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

Also, this code

另外,这段代码

- (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).

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

回答by WrightsCS

Your numberOfRowsInSection:section is not correct either; should be:

您的numberOfRowsInSection:部分也不正确;应该:

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

回答by WrightsCS

You should also use a switchstatement:

您还应该使用以下switch语句:

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

/* etc. */