如何在单击按钮时推送TableView并使用NSDictionary初始化TableViewCell?

时间:2012-01-17 09:56:59

标签: objective-c uitableview ios5 nsmutablearray

任何人都可以帮我解释如何在点击按钮时按下表格视图。 我想从NSMutableArray加载消息到表视图单元格,NSMutableArray加载从URL解析的数据..

-(IBAction)readMessages:(id)sender
{
    // i want to push the tableview when clicking the button in relation with this method
    // WHAT MUST I DO HERE?

   }

而不是问一个新问题,我喜欢编辑这个问题,因为事情是在同一个方面.. 我现在可以以编程方式创建tableview,但我无法使用从Json数组获得的数据初始化其单元格。这是我的代码:

NSString *str1=[@"?username=" stringByAppendingString:userNameField.text];
        NSString *str2=[@"&password=" stringByAppendingString:passwordField.text];
        NSString *str3=[str1 stringByAppendingString:str2];
        NSString *str4 =[@"http://" stringByAppendingString:serverField.text];


        NSURL *url=[NSURL URLWithString:[str4 stringByAppendingString:[@"/ipad/login.php" stringByAppendingString:str3]]];
        //get the url to jsondata
        NSData *jSonData=[NSData dataWithContentsOfURL:url];

        if (jSonData!=nil) {
            NSError *error=nil;
            id result=[NSJSONSerialization JSONObjectWithData:jSonData options:
                       NSJSONReadingMutableContainers error:&error];
            if (error==nil) {
                NSDictionary *mess=[result objectForKey:@"message"];
                NSDictionary *messContent=[mess valueForKeyPath:@"message"];
                NSDictionary *messID=[mess valueForKeyPath:@"ID"];
                NSString*key1=[ result objectForKey:@"key" ];                
                NSString *s1=[@"http://" stringByAppendingString:serverField.text];
                NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"];
                NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]];

                NSData *data2=[NSData dataWithContentsOfURL:url2];
                id result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];

                 mesID = [NSMutableArray array];//saving meesage ID s to NSMutableArray
                 content = [NSMutableArray array];    
               // i logged here and it saves  the data, now i want to display my data in table view

                for (NSDictionary *data in mess) {
                    [mesID addObject:[data objectForKey:@"ID"]];
                    [content addObject:[data objectForKey:@"message"]];        
                    [[NSUserDefaults standardUserDefaults] setObject:messID forKey:@"message"];
                    [[NSUserDefaults standardUserDefaults] setObject:messContent forKey:@"messContent"];

                    //messID will be saved as the Title of the cells and messContent will be displayed as the text area of that cell, opening in a new view

这是输出,我想将单元格的标题设置为ID,将其内容设置为文本:

  2012-01-17 16:26:59.873 ipad_Teslim[940:f803] MessID: (
        1,
        3
    )
    2012-01-17 16:26:59.875 ipad_Teslim[940:f803] Content: (
        asdf,
        "this is a test"
    )

正如我在我的代码中提到的那样,messID将被保存为单元格的标题,messContent将显示为该单元格的文本区域,以新视图打开。我现在该怎么办?请帮助我,那里有很多教程,我看了很多,但无法解决这个问题。

5 个答案:

答案 0 :(得分:2)

试试这个:

-(IBAction)readMessages:(id)sender {

    SecondView *secondView =[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];

    [self presentModalViewController:secondView animated:YES];    

  }

SecondView是你的UIViewController子类,它包含一个UITableView。

答案 1 :(得分:1)

如果您有导航控件,并且如果您想通过导航推送,请尝试以下操作:

-(IBAction)readMessages:(id)sender {
 NextView *next = [[NextView alloc]initWithNibName:@"NextView" bundle:nil];
        [self.navigationController pushViewController:next animated:YES];
        [next release];
}

如果你没有导航控件,如果你想只显示下一个视图,请尝试以下操作:

-(IBAction)readMessages:(id)sender {

    NextView *next =[[NextView alloc] initWithNibName:@"NextView" bundle:nil];

    [self presentModalViewController:next animated:YES]; 
           [next release];
  }

如果您在同一个班级中有子视图,请尝试以下操作:

-(IBAction)readMessages:(id)sender {

      [self.view addsubview nextView];
      }

答案 2 :(得分:1)

是的你可以, 尝试在viewDidload中以编程方式创建xib:

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 10,300,460)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];

但最好通过使用来创建 跟随xcode菜单中的路径:

文件>新 - > NewFile - > UIViewControllerSubClass - >下一步 - >下一步 - >创建

或简单地拖拽&从您的Interface Builder中删除视图

答案 3 :(得分:1)

Q1:您无需添加导航即可返回主页面。 什么时候使用

[self.navigationController pushViewController:next animated:YES];

默认情况下,它会在下一个视图中创建后退导航以推回返回。

如果尚未创建,请在下一个视图中尝试以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //To set the back buttin on leftside of Navigation bar
    UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(backclick:)] autorelease];
    self.navigationItem.leftBarButtonItem = backButton;

}

- (IBAction)backclick:(id)sender //first declrared in .h file
{
    // To goback to the previous view
    [self.navigationController popViewControllerAnimated:YES];
}

答案 4 :(得分:1)

Q2:你可以使用JSONArray初始化你的tableView单元格:

 (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [JSONarray count]; //***********
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


       cell.textLabel.text = [JSONarray objectAtIndexIndexPath.row]; //***********
}