Iphone SDK:编译器错误还是逻辑错误?

时间:2010-09-18 11:34:29

标签: iphone tableview

我创建了一个分组样式表视图

并且该表有5个部分,每个部分都有不同的行数

这是我的代码

1.设置表格中有多少部分

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 5;
}

2.设置部分中的每一行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    switch (section) {
  case 0:
   return 1;
   break;
  case 1:
   return 2;
   break;
  case 2:
   return 3;
   break;
  case 3:
   return 1;
   break;
  case 4:
   return 2;
   break;
  default:
   return 1;

 }
}

3.给出一个标题标题:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

 switch (section) {
  case 0:
   return @"Section 1";
   break;
  case 1:
   return @"Section 2";
   break;
  case 2:
   return @"Section 3";
   break;
  case 3:
   return @"Section 4";
   break;
  case 4:
   return @"Section 5";
   break;
  default:
   return nil;

 }
}

5.只在第一部分数据中给出第一行(因为其他人我还不知道设置单元格)

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
if (indexPath.section==0 && indexPath.row==0) {
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
  cell.textLabel.text = @"About System";

 }
    return cell;
}

比我在第0节Row0和第4节第0行

中得到两个相同的Cell

但如果我标记标题标题

它只出现在第0行第0行的文本标签

是我的开关逻辑错误?或者只是编译问题????

1 个答案:

答案 0 :(得分:1)

是的,这是一个逻辑错误。请注意,您“可能”正在重用-tableView:cellForRowAtIndexPath:中的单元格。因此,如果您没有设置文本标签,

  • 如果是全新的单元格,则文本标签将为空。

  • 如果是重复使用的单元格,文本标签将保持与之前相同的格式,可能是About System

总而言之,如果你没有设置标签,那么就没有明确的行为。

相关问题