UITableView 只推送到一个控制器
所以我有我的 UITableView,每个部分有 2 个部分和 1 个单元格,如果我单击第一个部分,它会起作用,然后单击第二个部分,它会转到第一个控制器。 RootViewController 是一个导航控制器,试图推送到 ViewController。
这是 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];
}
我在向部分添加更多部分和行/单元格时也遇到了麻烦。
谢谢。
So I have my UITableView, with 2 sections and 1 cell in each, and if I click the first one, it works, then the second one, it goes to the first controller. RootViewController is a navigationController, trying to push to ViewControllers.
Here's the code for the 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];
}
I'm also having trouble adding more sections and rows/cells to sections.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您应该在“didSelectRowAtIndexPath”中对indexPath.section而不是indexPath.row进行切换,因为两个部分都只有一行。
I believe you should do the switch in "didSelectRowAtIndexPath" over indexPath.section instead of indexPath.row since both sections have only one row.
每个部分中只有一行,因此 indexPath.row 将始终为零。在 didSelectRowAtIndexPath 中,您必须测试该部分而不是行(假设您打算每个部分仅保留一行)。
You only have one row in each section, so indexPath.row will always be zero. In didSelectRowAtIndexPath you have to test the section not the row (presuming you intend to keep only one row per section).