【问题标题】:nsmutablearray to tableviewnsmutablearray 到 tableview
【发布时间】:2013-03-26 00:37:22
【问题描述】:

我有一个 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 错误,其中创建了单元格并发出警告在同一行说 tableView 是本地和隐藏实例变量,有什么想法吗?感谢您的帮助!

更新: 我让我的程序编译,但它引发了一个异常: 2013-03-26 22:28:48.691 你好[79888:11303] * 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不是键值键 tableView 的编码兼容。'

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

【问题讨论】:

  • 您是否使用带有动态原型的情节提要作为表格视图?如果是这样,您不需要 cell = bit 并且该代码将被您的故事板覆盖。
  • 一方面,下载后不要调用 [tableView reloadData]。此外,您真的不应该在主线程上执行下载。我不知道你为什么会收到错误消息。您应该发布实际错误而不是解释。
  • 我还是新手,我应该在哪里添加 tableView reloadData?而且它也不是真正的错误,它只是停止应用程序说断点 1.1 但没有错误
  • 我也没有使用故事板我使用常规视图并在 xib 上添加了一个 tableview
  • 您的代码中是否设置了断点(行号上方有一个点的蓝色矩形)?您应该在 viewDidLoad 方法的末尾添加重新加载数据。

标签: iphone ios uiviewcontroller


【解决方案1】:

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

@synthesize tableView;

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

【讨论】:

    【解决方案2】:
    NSString *one;
    NSString *jsonreturn;
    

    这两种说法缺乏标准化。

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        查看你的 tablview 的变量名

        IBOutlet UITableView *tableView
        

        和 UITableView 的内置方法一样

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

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

        为避免这种情况,只需将您的 UITableView 变量名更改为“myTableView”

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2016-01-25
            • 2013-03-24
            • 2012-03-18
            • 2012-09-25
            • 2015-10-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多