1

2 つのセクションを持つ次のコードがあります。3 つのセルを含む最初のセクションにはテキストがあり、2 番目のセクションにはテキストがありません。問題は、セクション 2 の 6 番目程度のセルの後、セルがセクション 1 のテキストを繰り返すことです。

したがって、私のセルは次のようになります。セクション 1 - プロファイル、連絡先、設定。セクション 2 - 空白 空白、空白、空白、空白、プロファイル、連絡先、設定。

私は何を間違っていますか?

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0) return 3;
    else return 9;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"LeftMenuCell";
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(indexPath.section == 0) {
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"Profile";
            break;
        case 1:
            cell.textLabel.text = @"Contacts";
            break;
        case 2:
            cell.textLabel.text = @"Settings";
            break;

        default:
            break;
    }

    } else {
         //nothing should appear in these cells
    }

    return cell;
}
4

4 に答える 4

0

再利用中のセル。このdequeueReusableCellを参照してください。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

これは、Tableview Cell が再利用されるために発生するため cell.textLabel.text = @"";、else 条件にこれを追加する必要があります。つまり、第 2 セクションです。

于 2013-09-20T08:29:20.327 に答える