【问题标题】:UITableViewCell issue indexPath.row not displaying correctlyUITableViewCell 问题 indexPath.row 无法正确显示
【发布时间】:2011-04-28 00:27:18
【问题描述】:

我设置了一个 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];

这一切都很好,直到今天晚上我在行中文本的左侧添加了图像。图像显示完美,但由于某种原因,这些行都填充了饮料数据中的对象,它完全忽略了其他两组对象。代码在我用过的下面。我希望它就像错误地编写 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;

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

谢谢

【问题讨论】:

    标签: objective-c xcode ios uitableview


    【解决方案1】:

    Objective-c 结构不是基于缩进的

    这个:

    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 适用于下一条语句。如果您需要多个语句,则需要将语句括在花括号中。你也可以一直用卷发来不用担心。

    还有,这段代码

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

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

    【讨论】:

    • 感谢您的回复。当我按照您的建议输入花括号并遵循它时,结果保持不变。所有行仍然填充有饮料数据对象。
    • 你是否为所有你需要的地方做了它(不仅仅是我指出的那个)
    • 是的,我一直带着它。然而,我后退了一步,想了想,它立即点击了我出错的地方。最后我放了 return cell;} 而不是花括号 } return cell;所以它将单元格设置为最后一个 cell.textLabel settext 是什么。现在已经整理好了,谢谢你的帮助:)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您还应该使用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. */
      

      【讨论】:

      • 您好,感谢您的回复。我已更改我的 numberOfRowsInSection 以反映您的建议。但是,该 switch 语句似乎没有任何区别。不确定我是否错误地实现了它,但是当我遵循它时 - case0: case 1: etc,当我编译并尝试使用应用程序上的 UITableView 调用该视图时终止。
      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2017-01-25
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多