【发布时间】: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