views:

26

answers:

1

I create a grouped style tableview

and the table has 5 sections ,and each section has different numbers of rows

Here is my code

1.Set how many sections in a table

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

2.Set each rows in a section

- (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.Give a section header :

- (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.Only give first row in first section data(because others I'm not have idea to set the cell yet)

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

than I got two same Cell in Section 0 Row0,and Section 4 Row 0

But If I mark the section header

It only appear a text label at Section 0 Row 0

IS my switch logic error ? or just compiler issue ????

+1  A: 

Yes it's a logic error. Notice that you are "probably" reusing a cell in -tableView:cellForRowAtIndexPath:. Therefore, if you don't set the text label,

  • if it is a brand new cell, the text label will be empty.

  • if it is a reused cell, the text label will remain the same as previously, which may be About System or not.

To conclude, if you don't set the label, there is no defined behavior on what you will get.

KennyTM
but... it still make no sence when I only remove - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section...than the cell become the result I want it to.
WebberLai
@WebberLai: Right. The code currently makes no sense besides Section 0 Row 0.
KennyTM