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



有人能帮助我了解如何在单击按钮时推送表视图吗。我想将消息从NSMutableArray加载到表视图单元格,并且NSMutaableArray加载了从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?
   }

与其问一个新问题,我更喜欢编辑这个问题,因为事情是在同一方面。。我现在可以用程序创建表视图,但我不能用从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将显示为该单元格的文本区域,在新视图中打开。。我现在该怎么做?请帮帮我,那里有很多教程,我也看了很多,但无法解决这个问题。

试试这个:

-(IBAction)readMessages:(id)sender {
    SecondView *secondView =[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
    [self presentModalViewController:secondView animated:YES];    
  }

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

Q1:U无需添加导航即可返回主页。当你使用时

[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];
}

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

-(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];
      }

是的,U可以,尝试在viewDidload:中创建xib程序

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

但最好使用xcode菜单中的以下路径:

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

或者简单地拖动&从ur Interface Builder 中删除视图

Q2:U可以使用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]; //***********
}

最新更新