我如何命名单元格导航控制器?

时间:2011-07-20 13:14:09

标签: iphone objective-c uitableview uinavigationcontroller

 if (indexPath.row % 2 == 0) {
    // EVEN
    cell = [tableView dequeueReusableCellWithIdentifier:@"EvenCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EvenCell"] autorelease];
        UIView *bg = [[UIView alloc] initWithFrame:cell.frame];

        UIColor *colour = [[UIColor alloc] initWithRed: (208.0/255.f) green: (231.0/255.f) 
                                                  blue: (241.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour; 
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
        cell.textLabel.text = [items objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

} else {
    // ODD

    cell = [tableView dequeueReusableCellWithIdentifier:@"OddCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OddCell"] autorelease];
        UIView *bg = [[UIView alloc] initWithFrame:cell.frame];

        UIColor *colour = [[UIColor alloc] initWithRed: (143.0/255.f) green: (169.0/255.f) 
                                                  blue: (180.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour;
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
        cell.textLabel.text = [items objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
} 

return cell;
}

这是我的自定义单元格。第一个是偶数第二个是奇数。我的数组有194个元素但是当我运行应用程序时我只能在第10个单元后看到10它再次转到第一个元素给我相同的10个元素。谁能告诉我这里有什么问题?

2 个答案:

答案 0 :(得分:2)

试试这个。

if (indexPath.row % 2 == 0) {
// EVEN
        cell = [tableView dequeueReusableCellWithIdentifier:@"EvenCell"];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EvenCell"] autorelease];
            UIView *bg = [[UIView alloc] initWithFrame:cell.frame];


        UIColor *colour = [[UIColor alloc] initWithRed: (208.0/255.f) green: (231.0/255.f) 
                                                  blue: (241.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour; 
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
    }
        cell.textLabel.text = [items objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


} else {
    // ODD

    cell = [tableView dequeueReusableCellWithIdentifier:@"OddCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OddCell"] autorelease];
        UIView *bg = [[UIView alloc] initWithFrame:cell.frame];

        UIColor *colour = [[UIColor alloc] initWithRed: (143.0/255.f) green: (169.0/255.f) 
                                                  blue: (180.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour;
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
    }
        cell.textLabel.text = [items objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


  } 

  return cell;
 }

答案 1 :(得分:1)

为了让您的视图显示正确的行数,请确保-(NSUInteger)tableView:numberOfRowsInSection:(NSUInteger)section返回正确的数字。 (在您的情况下,您应该返回[items count])。