1

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

我尝试使用 if else 语句 id my 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];
}

}
4

2 回答 2

0

注意:你想创建和显示 UIView,所以我把UIViewNot 放在这里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];
        }
    }
于 2013-02-11T10:19:09.067 回答
0
  1. 如果您希望显示视图,您可以声明一个 Mutable 数组并在其中添加 UIview,如果 UIview 内容具有不同类型的内容样式。

  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. 如果要显示不同的 ViewController,则需要执行以下代码

    #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];
    
      }
    
于 2013-02-11T10:40:48.817 回答