【问题标题】:UITableView only pushes to one controllerUITableView 只推送到一个控制器
【发布时间】:2011-01-16 09:38:49
【问题描述】:

所以我有我的 UITableView,每个部分有 2 个部分和 1 个单元格,如果我单击第一个,它可以工作,然后第二个,它转到第一个控制器。 RootViewController 是一个navigationController,试图推送到ViewControllers。

这是 tableView 的代码:

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
        return 1;
    else
        return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return @"Terminal/SSH Guides";
    }else{
        return @"Cydia Tutorials";
    }
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    if(indexPath.section == 0){
        cell.text = @"Changing Password for root";
    } else {
        cell.text = @"Hiding Sections";
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    id newController;
    switch (indexPath.row) {
        case 0:
            newController = [[rootpassword alloc] initWithNibName:@"rootpassword" bundle:nil];
            break;
        case 1:
            newController = [[hidingsections alloc] initWithNibName:@"hidingsections" bundle:nil];
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:newController animated:TRUE];
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
}

我也无法将更多部分和行/单元格添加到部分。

谢谢。

【问题讨论】:

    标签: iphone objective-c uitableview navigationcontroller


    【解决方案1】:

    我认为您应该在 indexPath.section 而不是 indexPath.row 上的“didSelectRowAtIndexPath”中进行切换,因为这两个部分只有一行。

    【讨论】:

    • 这似乎可行,但如果我想为每个部分添加更多部分和多行怎么办?谢谢。
    • 有很多方法可以做到这一点,具体取决于您拥有的数据。通常,您将在部分上打开一个数组 foreach 部分,其中包含用于配置单元格的数据。查看此示例mobisoftinfotech.com/blog/iphone/introduction-to-table-view
    • 啊,谢谢。设法让它与一些 if/switch 循环一起工作。
    【解决方案2】:

    每个部分只有一行,因此 indexPath.row 将始终为零。在 didSelectRowAtIndexPath 中,您必须测试部分而不是行(假设您打算每个部分只保留一行)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-07
      • 2015-06-13
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 2015-06-26
      相关资源
      最近更新 更多