开发者

UITableViewCell issue indexPath.row not displaying correctly

开发者 https://www.devze.com 2023-03-01 17:32 出处:网络
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;

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

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 objectAtIn开发者_JAVA百科dex: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.

Thanks


Objective-c structure isn't based on indentation

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.

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


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

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


You should also use a switch statement:

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. */
0

精彩评论

暂无评论...
验证码 换一张
取 消