【问题标题】:XCode, table view cellForRowAtIndexPathXCode,表格视图 cellForRowAtIndexPath
【发布时间】: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


【解决方案1】:

你确定 bookName 是一个有效的 NSString 吗?我会打破那行代码并输入

po bookName

在调试器中查看您分配给 bookName 的内容是否实际上是一个有效的 NSString。

哦,请确保在该方法结束时返回有效的 UITableViewCell,否则保证您的代码会中断。

【讨论】:

  • 您使用的是最新的 Xcode 版本并且使用的是 ARC 吗?我假设你不是因为autorelease 不会编译
【解决方案2】:

在你的函数中用这个代码替换 BooksBorrowed = newBooksBorrowed;

BooksBorrowed = [[NSString alloc] initWithArray:newBooksBorrowed];

希望对您有所帮助。快乐的编码.. 谢谢。

【讨论】:

  • 请发布您的 tableview 方法.. 并且您是否检查过该数组是否填充了数据?你初始化数组了吗?@user2176687
  • 是的,在我分配单元格内容之前创建的日志中,它应该是这样的
  • newBooksBorrowed 是一个数组
【解决方案3】:

我的问题的解决方案是:

我必须将我将数据添加到表中的部分更改为:

 NSString *string = [[NSString alloc] initWithFormat:@"%@",[BooksBorrowed objectAtIndex:indexPath.row]];

这是因为表格视图倾向于不获取元素 0 的内容。我还取出了 newBooksBorrowed,它仅适用于 BooksBorrowed 数组。 :) 谢谢大家帮我解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    相关资源
    最近更新 更多