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

发布于 2024-12-27 07:41:05 字数 3101 浏览 3 评论 0原文

任何人都可以帮助我了解如何在单击按钮时推送表格视图。 我想将消息从 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 将是显示为该单元格的文本区域,在新视图中打开。我现在该怎么做?请帮助我,那里有很多教程,我也看了很多,但无法解决这个问题。

Can anyone please help me about how to push a table view on clicking a button.
I want to load the messages from NSMutableArray to the table view cells and NSMutableArray is loaded with the data parsed from a 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?

   }

Instead of asking a new question i liked to edit this one, since the matter is in the same aspect..
I now can create the tableview programatically, but i cant initialize its cells with the data i get from Json array. Here is my code:

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

And this is the output, i want to set the titles of cells as ID and their content as text:

  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"
    )

As i have mentioned in my code too, 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.. How can i do it now? Please Help me, there are a lot of tutorials there, i looked a lot too but couldn't break this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简单爱 2025-01-03 07:41:05

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

Q1: U no need to add a navigation to return back to ur main page.
When ever u use

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

by defaults it will creates back navigation in the next view to push return back.

in case it doesn't created yet, Try the following code in next view:

- (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];
}
白衬杉格子梦 2025-01-03 07:41:05

试试这个:

-(IBAction)readMessages:(id)sender {

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

    [self presentModalViewController:secondView animated:YES];    

  }

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

Try this :

-(IBAction)readMessages:(id)sender {

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

    [self presentModalViewController:secondView animated:YES];    

  }

SecondView is your UIViewController subclass which hold a UITableView.

梦在夏天 2025-01-03 07:41:05

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

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

If u have a navigation control and if u want to pushed by navigation try the following:

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

if u dont have a navigation control and if u want to just display the next view, try the following:

-(IBAction)readMessages:(id)sender {

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

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

if u are having sub view in the same class try the following:

-(IBAction)readMessages:(id)sender {

      [self.view addsubview nextView];
      }
温柔戏命师 2025-01-03 07:41:05

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

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

但最好使用以下方法创建
xcode 菜单中的以下路径:

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

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

Yes U can,
try this to create xib programitically in viewDidload:

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

But better to create by using
following path in xcode menu:

File-> New -> NewFile -> UIViewControllerSubClass -> Next -> Next -> Create

Or simply Drag & drop an view from ur Interface Builder

清浅ˋ旧时光 2025-01-03 07:41:05

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

Q2: U can initialize ur tableView cells with JSONArray:

 (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]; //***********
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文