nsmutablearray到tableview

时间:2013-03-26 00:37:23

标签: iphone ios uiviewcontroller

我有一个nsmutablearray,我试图在桌面视图上显示。我有一个uiviewcontroller并手动添加表视图。我已经通过将它添加到文件所有者的xib上添加了委托和源代码。这是我的.h文件

#import <UIKit/UIKit.h>   

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
    IBOutlet UILabel *label;
    IBOutlet UITextField *texto;
    IBOutlet UIWebView *link;
    //IBOutlet UILabel *links;
    IBOutlet UITextView *links;    
    // IBOutlet UITableView *tableView;
    NSMutableArray *jsonArray;
}

@property (nonatomic,retain) IBOutlet UITableView *tableView;
//@property (nonatomic,retain) NSMutableArray *jsonArray;

-(IBAction)button;
-(IBAction)field;

-(void)populateArray;

@end 

这是我的.m文件

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

NSString *one;
NSString *jsonreturn;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url = [NSURL URLWithString:@"www.myphpfile.com"];
    jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
NSLog(jsonreturn); // Look at the console and you can see what the restults are
    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
    NSError *error = nil;
    jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];

    NSLog(@"jsonList: %@", jsonArray);

    if(!jsonArray)
    {
        NSLog(@"Error parsing JSON:%@", error);
    }
    else
    {
        // Exception thrown here.
        for(NSDictionary *item in jsonArray)
        {
            NSLog(@"%@", item);
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)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];
    }

    NSLog(@"Im HERE!!!");

    cell.textLabel.text = [jsonArray objectAtIndex:[indexPath row]];
    NSLog(@"Cell is %@", [jsonArray objectAtIndex:indexPath.row]);
    return cell;
}

我的问题是,我不能让单元格显示日期,我有ns日志,它显示它正在拉动它,我也得到一个线程1,断点1.1错误,其中创建单元格和警告在同一行中sayin tableView是本地隐藏实例变量,有什么想法吗?谢谢你的帮助!

更新: 我得到了我的程序编译,但它抛出一个异常: 2013-03-26 22:28:48.691 Hello [79888:11303] * 由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是键值符合编码的密钥tableView。'

这是否与表格中的信息有关或我创建表格错误?再次感谢!

5 个答案:

答案 0 :(得分:0)

如果您已通过界面构建​​器/故事板添加了tableview,请尝试添加:

@synthesize tableView;

在.m文件中的@implementation下。然后确保您在IB中正确连接了tableview。 (以View Controller作为数据源和委托,然后将VC形成为tableview作为tableView。)

答案 1 :(得分:0)

NSString *one;
NSString *jsonreturn;

这两个陈述缺乏标准化。

如果你没有使用ARC,jsonreturn会泄漏。

答案 2 :(得分:0)

您收到警告,因为您有一个名为tableView的实例变量,并且在表视图中,数据源方法tableView作为参数传入。更改此变量名称或实例变量名称可以解决问题。既然它们都指的是同一个东西,那也没关系。

如果它只是停在Stopping at Breakpoint 1点,你可能已经设置了一个断点。它看起来像一个蓝色箭头标记,位于代码的左侧。您可以通过再次单击它(变为较浅的阴影)或通过删除它来禁用它 - 拖出断点或右键单击断点并删除。

Breakpoint

答案 3 :(得分:0)

查看tablview的变量名称

IBOutlet UITableView *tableView

与UITableView的构建方法相同

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

这就是为什么它给你警告,“tableView是本地的并隐藏实例变量”

要避免它只是将您的UITableView变量名更改为“myTableView”

答案 4 :(得分:0)

首先分配你的NSMutableArray,像这样“ArrayName = [[NSMutableArray alloc] init];