【发布时间】:2013-03-22 16:21:06
【问题描述】:
我知道有一个简单的解决方案,但我似乎无法判断:/
cell.textLabel.text = [BooksBorrowed objectAtIndex:0];
我的 cellForRowAtIndexPath 方法中有这个,bookName 是一个字符串。 它崩溃并且在日志中没有留下任何错误。我不知道我做错了什么。
bookName 是一个字符串,我从 JSON Parsing 中获得并包含内容。
(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];
}
NSLog(@"Book Name is %@", BooksBorrowed[0]);
cell.textLabel.text = [BooksBorrowed objectAtIndex:0];
return cell;
}
这就是我获得 BooksBorrowed 数组的方式:
- (void)updateMyBooks
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Fetch data on a background thread:
NSString *authFormatString =
@"http://localhost:8888/Jineel_lib/bookBorrowed.php?uid=%d";
NSString *urlString = [NSString stringWithFormat:authFormatString, 1];
NSURL *url = [NSURL URLWithString:urlString];
NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
response1 = [contents JSONValue];
if (contents) {
NSMutableArray *newBooksBorrowed = [NSMutableArray array];
// ... Parse JSON response and add objects to newBooksBorrowed ...
BookName = [[NSString alloc]init];
DateBorrowed = [[NSString alloc]init];
BookID = [[NSString alloc]init];
BookExtended = [[NSString alloc]init];
BookReturned = [[NSString alloc]init];
BookName = [response1 valueForKey:@"BookName"];
BookID = [response1 valueForKey:@"BookID"];
DateBorrowed = [response1 valueForKey:@"DateBorrowed"];
BookExtended = [response1 valueForKey:@"Extended"];
BookReturned = [response1 valueForKey:@"Returned"];
NSLog(@"Book Name is %@", BookName);
[newBooksBorrowed addObject:BookName];
dispatch_sync(dispatch_get_main_queue(), ^{
// Update data source array and reload table view.
BooksBorrowed = newBooksBorrowed;
[self.tableView reloadData];
});
}
});
}
【问题讨论】:
-
这行代码没有问题。尝试将 cellForRowAtIndexPath 中的所有代码
-
...以及如何创建/存储 bookName,以及是否使用 ARC
-
请参考已编辑的问题
标签: xcode uitableview