1
我正在第一次在目标c中工作,并遇到了一个问题,我没有看到答案。可可触摸UITableView数据
我从JSON数据集中加载一组数据并使用它来填充UITableViewController中的UITableView。
首先加载视图(viewDidLoad)时,我使用来自URL的JSON数据填充数组。
接下来的数据加载,因为它应该。 numberOfRowsInSection显示数组中有30个结果是正确的。
但是Iphone将整个集合加载到tableview中三次。
这里是从控制器该视图一些代码:(Twitter正在被用作示例,并且是未设置我使用的实际数据)
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//results = [[NSMutableArray alloc] init];
[email protected]"public_timeline";
self.tableView.allowsSelection = NO;
NSString *url = [[NSString alloc] init];
url = [[NSString alloc] initWithFormat:@"http://twitter.com/statuses/%s.json",@"public_timeline"];
if ([results count] == 0) {
[self parseJSON:url];
}
}
Here is the parseJSON (actual parse is done with the Cocoa JSON framework
-(void) parseJSON:(NSString *)URL{
NSURL *JSONURL = [NSURL URLWithString:URL];
NSData *responseData = [[NSData alloc] initWithContentsOfURL:JSONURL];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
results = [[NSMutableArray alloc] initWithArray:[responseString JSONValue]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [results 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];
// Set up the cell...
}
NSDictionary *cdict = [results objectAtIndex:indexPath.row];
cell.textLabel.text = [cdict valueForKey:@"text"];
return cell;
}
我不知道我正在做的是做到这一点的最佳方式,所以如果有人能帮助我,这将是伟大的。
“负荷三次”你的意思是你要每个表观察室3份在你的表视图?或者,你是否目睹了你的桌面加载之前你的断点被击中了3次? – Meltemi 2009-10-01 04:42:37
3数据副本显示在视图单元格中。 – lrudor 2009-10-01 22:04:28