2013-03-20 44 views
1

我知道有一個簡單的解決辦法,但我似乎無法能夠告訴:/的XCode,表視圖的cellForRowAtIndexPath

cell.textLabel.text = [BooksBorrowed objectAtIndex:0]; 

我有我的cellForRowAtIndexPath方法,BOOKNAME是一個字符串。 它崩潰並在日誌中沒有錯誤。我不知道我做錯了什麼。

bookName是我從JSON解析並獲得內容的字符串。

(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]; 
     }); 
    } 
}); 

}

+2

這行代碼沒有問題。嘗試從cellForRowAtIndexPath – 2013-03-20 11:54:06

+0

...以及如何創建/存儲bookName以及您是否使用ARC或 – Vladimir 2013-03-20 11:54:48

+0

的所有代碼請參閱編輯的問題 – 2013-03-20 12:19:59

回答

0

我的問題的解決方案是:

我必須改變,我加入我的數據到表中的一部分:

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

這是因爲表視圖往往用不了的東西元素0.此外,我拿出newBooksBorrowed,它只與BooksBorrowed數組一起工作。 :)謝謝大家幫我解決它。

0

你肯定BOOKNAME是有效的NSString?我將打破在該行的代碼,並在調試器中鍵入

po bookName 

,看看你是什麼分配給bookName實際上是一個有效的NSString。

哦,並確保您在該方法的末尾返回一個有效的UITableViewCell,否則它會保證您的代碼會中斷。

+0

請參閱編輯的問題 – 2013-03-20 12:20:36

+0

您是否在最新的Xcode版本和你在使用ARC嗎?我假設你不是因爲'autorelease'不會編譯 – 2013-03-20 12:45:57

+0

不,它是最新的Xcode版本 – 2013-03-21 09:02:43

0

替換此代碼BooksBorrowed = newBooksBorrowed;這個在你的函數

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

希望它能幫助。快樂編碼.. 謝謝。

+0

請參考已編輯的問題 – 2013-03-20 12:21:02

+0

請發佈你的tableview方法..並且你檢查過數組是否填充了數據?你有初始化數組嗎?@ user2176687 – Dhruvik 2013-03-20 12:24:04

+0

是的,在日誌中,我只是在分配單元格內容之前,它出來應該是 – 2013-03-20 12:29:57