tableview根据单击的行打开不同的视图

时间:2013-02-11 10:14:35

标签: objective-c xcode uitableview

我正在创建一个带有tableview的应用程序现在我想要发生的是当我点击第一行时它将显示view1,当我点击第二行时它将显示view2。我怎么能这样做?

我尝试使用if else语句id didSelectRowAtIndexPath,但它在这里不起作用是我的代码

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

int EntryIndex = [indexPath indexAtPosition:[indexPath length]-1];

{
    AboutViewController *aboutView = [[AboutViewController alloc]init];
    aboutView.getId4 = [[contentArray objectAtIndex:EntryIndex]objectForKey:@"title"];
    aboutView.aboutofficeId = getId3;
     [self.navigationController pushViewController:aboutView animated:YES];
}
else
{
    DepartmentsViewController *deptView = [[DepartmentsViewController alloc]init];
    deptView.getId5 = [[contentArray objectAtIndex:EntryIndex]objectForKey:@"title"];
    deptView.deptoffice = getId3;
    [self.navigationController pushViewController:deptView animated:YES];
}

}

2 个答案:

答案 0 :(得分:0)

注意:您想要创建并显示UIView,因此我在这里推出UIView而不是UIViewController

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

        if(indexPath.row == 1)
        {
            UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake("AsYouWnat")];
            view1.backgroundColor = [UIColor redColor];
            [self.view addSubview:view1];
        }
        else if(indexPath.row  == 2)
        {
            UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake("AsYouWnat")];
            view2.backgroundColor = [UIColor redColor];
            [self.view addSubview:view2];
        }
        .
        .
        .
        .
        .
        ////////// As Your Row In Table
        else
        {
            UIView *viewN = [[UIView alloc] initWithFrame:CGRectMake("AsYouWnat")];
            viewN.backgroundColor = [UIColor redColor];
            [self.view addSubview:viewN];
        }
    }

答案 1 :(得分:0)

  1. 如果您希望显示视图如果UIview内容具有不同类型的内容样式,您可以声明一个Mutable数组并添加UI视图。

  2. 如果您需要显示相同风格的视图,但只有内容不同。您可以查找以下代码。

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,460)];
    switch (indexPath.row) {
    case 0:
    { 
    //Add your label,image,etc  
    [self.view addSubview:view];
    }
        break;
    
        case 1:
    {
    
    //Add your label,image,etc  
    [self.view addSubview:view];
    
    }
        break;
    
  3. 如果您想显示不同的ViewControllers,那么您需要执行以下代码

    #import "ViewController1.h"
    #import "ViewController2.h"
    #import "ViewController3.h"
    
    -(void)viewDidLoad
    {
    
     VCArray=[[NSMutableArray alloc]initWithObjects:ViewController1,ViewController2,ViewController3];//allocate and init ViewController and add that to VCArray
    
     }
    
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
     UIViewController *viewcontr=[VCArray objectAtIndex:indexpath.row];
     viewcontr=[[viewcontr alloc]init];
     [self presentModalViewController:viewcontr animated:YES];
    
      }