Iphone uitableview分区有问题-每个分区中的单元格都在重复

Iphone uitableview分区有问题-每个分区中的单元格都在重复,iphone,ios,uitableview,Iphone,Ios,Uitableview,提前感谢您的帮助,我很难理解代码。我试图将一个运行良好的现有uitable分为4个部分。正如您所看到的,我有4个部分,每个部分中有不同数量的行和每个部分的名称。编译时,节正确地存在,但单元格重复。我尝试过其他帖子中关于这个主题的各种方法,也尝试过谷歌搜索的信息,但我不断收到大量错误。如果有人能告诉我我遗漏了什么,我将不胜感激!谢谢 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return t

提前感谢您的帮助,我很难理解代码。我试图将一个运行良好的现有uitable分为4个部分。正如您所看到的,我有4个部分,每个部分中有不同数量的行和每个部分的名称。编译时,节正确地存在,但单元格重复。我尝试过其他帖子中关于这个主题的各种方法,也尝试过谷歌搜索的信息,但我不断收到大量错误。如果有人能告诉我我遗漏了什么,我将不胜感激!谢谢

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

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
    case 0:
        return 2;
        break;
    case 1:
        return 3;
        break;
    case 2:
        return 2;
        break;
    case 3:
        return 1;
        break;
}
return 0;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *sectionHeader = nil;

if(section == 0) {
    sectionHeader = @"Section 1";
}
if(section == 1) {
    sectionHeader = @"Section 2";
}
if(section == 2) {
    sectionHeader = @"Section 3";
}
if(section == 3) {
    sectionHeader = @"Section 4";
}

return sectionHeader;
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];   
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    [cell.textLabel setNumberOfLines:2];
}

cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.detailTextLabel.font = [UIFont systemFontOfSize:11.5];

switch (indexPath.row) {
     // I would like these in Section One
    case 0:
        [cell.textLabel setText:@"Cell one"];
        [cell.detailTextLabel setText:@"subtext one"];
        break;
    case 1:
        [cell.textLabel setText:@"Cell two"];
        [cell.detailTextLabel setText:@"subtext two"];
        break;
 // And these in Section Two
    case 2:
        [cell.textLabel setText:@"Cell three"];
        [cell.detailTextLabel setText:@"subtext three"];
        break;
    case 3:
        [cell.textLabel setText:@"Cell four"];
        [cell.detailTextLabel setText:@"subtext four"];
        break;
    case 4:
        [cell.textLabel setText:@"Cell five"];
        [cell.detailTextLabel setText:@"subtext five"];
        break;
  // And these in Section Three
    case 5:
        [cell.textLabel setText:@"Cell six"];
        [cell.detailTextLabel setText:@"subtext six"];
        break;
    case 6:
        [cell.textLabel setText:@"Cell seven"];
        [cell.detailTextLabel setText:@"subtext seven"];
        break;
  // And this in Section four
    case 7:
        [cell.textLabel setText:@"Cell eight"];
        [cell.detailTextLabel setText:@"subtext eight"];
        break;
    default:
        break;
}

return cell;        
}
您必须添加一个

switch (indexPath.section)
在cellForRowAtIndexPath中

或者一个if-else子句

大概是这样的:

if (indexPath.section == 0 && indexPath.row == 0)
{
     // for the first row in the first section
}
else if (indexPath.section == 3 && indexPath.row == 2)
{
    // for the 4 section and 3 row
}
您必须添加一个

switch (indexPath.section)
在cellForRowAtIndexPath中

或者一个if-else子句

大概是这样的:

if (indexPath.section == 0 && indexPath.row == 0)
{
     // for the first row in the first section
}
else if (indexPath.section == 3 && indexPath.row == 2)
{
    // for the 4 section and 3 row
}