【问题标题】:How can i push a TableView on clicking a button and initialize the TableViewCell with NSDictionary?如何在单击按钮时推送 TableView 并使用 NSDictionary 初始化 TableViewCell?
【发布时间】:2012-01-17 09:56:58
【问题描述】:

任何人都可以帮助我了解如何在单击按钮时推送表格视图。 我想将 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?

   }

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

【问题讨论】:

  • 顺便说一句,我在我的班级的不同方法中使用上面的代码

标签: objective-c uitableview ios5 nsmutablearray


【解决方案1】:

试试这个:

-(IBAction)readMessages:(id)sender {

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

    [self presentModalViewController:secondView animated:YES];    

  }

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果你有一个导航控件,如果你想通过导航推送,请尝试以下操作:

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

      【讨论】:

      • 对不起,我在 Objective C 方面有点缺乏经验。我没有任何 xib 文件,我想以编程方式创建视图。我需要创建一个 xib 文件来使用这些代码吗?
      • 从上午 10 点到下午 6 点(印度时间)寻求任何帮助。选择勾选以上任何一个答案,您最好通过提高接受率来获得快速响应
      • 谢谢@Prasad,我在土耳其,事实上我无法想象现在在你家几点了。我想大约是晚上 8 点到 9 点:)
      • @prasad 提供您的电话号码,这将有助于我们随时与您联系
      【解决方案4】:

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

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

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

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

      或者简单地从你的 Interface Builder 中拖放一个视图

      【讨论】:

      • 您可以编辑现有答案而不是创建新答案。
      • Thnks @vin,但我使用这种方式,因为 ilhan çetin 是新用户.. 让他读起来像这样写。再次感谢
      • 是的,您也可以通过帮助他人了解您的答案来提高自己和他人的技能。欢迎
      • 我用你的代码创建了一个 UITableView(不是 UIView)。现在我有两件事:一,如何添加导航控制器以返回我的主页?二:如何用我的 JSON 数组初始化这个 tableviewcells(我已经解析了 JSON 并在 NSMutableArray 中初始化)?我的问题可能会很无聊,但我真的无法解决..
      【解决方案5】:

      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]; //***********
      }
      

      【讨论】:

      • 但是我用不同的方法解析数据,当我尝试这个时,它会在 NSLog() 中带来一些奇怪的东西,而在单元格中什么都没有。我还没有尝试过你的这个解决方案,我会拒绝你。谢谢
      猜你喜欢
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 2018-07-02
      相关资源
      最近更新 更多