我在应用程序上设置了一个分组的表,并且推动新视图效果很好。除了一件事。标题和堆栈布局都很奇怪。这是分解:

  1. 我的桌子上有两个部分。
  2. 当我在第一部分中点击第一行时,它将我带到正确的视图,但是新视图的标题是表的第二部分中的第一行。
  3. 反过来,第一部分的标题中的第二行是第二部分标题中的第二行。

如果我点击根视图表的第二部分中的第二行,则导航按钮进入第二行 第一的 表的部分。

所以这是我的桌子的图:


表第1部分
第1行
第2行
第3行

表第2部分
第a行
行b
行c


因此,如果我点击第3行,则推出视图的标题是行C。导航按钮告诉我返回第3行,然后最终在根视图中最终出现。

这是我的实现文件推出视图:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //CSS
    if ([[arryClientSide objectAtIndex:indexPath.row] isEqual:@"CSS"])
    {
        CSSViewController *css = [[CSSViewController alloc] initWithNibName:@"CSSViewController" bundle:nil];
        [css setTitle:@"CSS"];
        [self.navigationController pushViewController:css animated:YES];
    }

    //HTML
    if ([[arryClientSide objectAtIndex:indexPath.row] isEqual:@"HTML"])
    {
        HTMLViewController *html = [[HTMLViewController alloc] initWithNibName:@"HTMLViewController" bundle:nil];
        [html setTitle:@"HTML"];
        [self.navigationController pushViewController:html animated:YES];
    }

    //JS
    if ([[arryClientSide objectAtIndex:indexPath.row] isEqual:@"JavaScript"])
    {
        JSViewController *js = [[JSViewController alloc] initWithNibName:@"JSViewController" bundle:nil];
        [js setTitle:@"JavaScript"];
        [self.navigationController pushViewController:js animated:YES];
    }

    //PHP
    if ([[arryServerSide objectAtIndex:indexPath.row] isEqual:@"PHP"])
    {
        PHPViewController *php = [[PHPViewController alloc] initWithNibName:@"PHPViewController" bundle:nil];
        [php setTitle:@"PHP"];
        [self.navigationController pushViewController:php animated:YES];
    }

    //SQL
    if ([[arryServerSide objectAtIndex:indexPath.row] isEqual:@"SQL"])
    {
        SQLViewController *sql = [[SQLViewController alloc] initWithNibName:@"SQLViewController" bundle:nil];
        [sql setTitle:@"SQL"];
        [self.navigationController pushViewController:sql animated:YES];
    }

&馈送表数据的数组:

- (void)viewDidLoad {
    [super viewDidLoad];


    arryClientSide = [[NSArray alloc] initWithObjects:@"CSS",@"HTML",@"JavaScript",nil];
    arryServerSide = [[NSArray alloc] initWithObjects:@"Objective-C", @"PHP",@"SQL",nil];
    // arryResources = [[NSArray alloc] initWithObjects:@"HTML Colour Codes", @"Useful Resources", @"About",nil];
    self.title = @"Select a Language";
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

任何帮助将是 很大 感谢。
杰克

有帮助吗?

解决方案

我认为您缺少IF语句来确定用户点击哪个部分。请记住,NSINDEXPATH具有行和部分属性。我通常做这样的事情:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ( indexPath.section == 0 ) {
  // Do something with arryClientSide
}
if ( indexPath.section == 1 ) {
  // Do something with arryServerSide
}

我假设您正在使用分组表。

欢呼, - 丹

其他提示

DAN是对的,您需要使用IndexPath。节变量。看来您的代码现在的方式,您实际上是在推动2个视图控制器,因为您有一系列个人IF语句,单击“第1行”后,以下发生了以下情况:

iSequal:@“ CSS”将返回true,

推CSS视图控制器,

然后下拉并继续检查(如果.. iSequal:@“ html”)..(如果.. iSequal:@“ javascript”)

然后,肯定(如果.. iSequal:@“ php”)也将返回真实并推动PHP视图控制器。

我不知道您的整个应用程序,但是我在这里注意到的一个观察(除了内存泄漏)是您有一个专用于每种语言的视图控制器/笔尖。.如果视图控制器的行为/外观差异很大,那么我想这就是您的必须这样做,但我建议尝试查看每一个之间的相似之处,看看您是否不能制作单个“ LagingviewController”,请传递其应表示的语言,并进行初始化/设置视图控制器的属性基于这一点。这可能不适用,但是如果这样做可以为您节省很多工作!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top