我有一個表格視圖似乎正在工作,但它不像預期的那樣在單元格中顯示文本。換句話說,當我加載tableView時,它只顯示空白單元格,而我希望一個單元格顯示下面代碼中請求的文本。UITableView單元格不按預期方式顯示文本
我的代碼下面有什麼問題嗎?
注意:我只實現cellForRowAtIndexPath,因爲我的tableView是斯坦福大學的CoreDataTableViewController
的一個子類。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"a_cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
NSLog(@"cell is nil");
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
//Page is a managed object entity with a name attribute.
Page *page = [self.fetchedResultsController objectAtIndexPath:indexPath];
//The log below returns the expected string for the name value.
NSLog(@"page.name %@", page.name);
cell.textLabel.text = @"page.name";
return cell;
}
只有一個對象或「頁」截至目前,所以我期望只有一個的tableView細胞(頂部)來顯示文本。其實一切都是空白的。
當我登錄使用indexPath:
NSLog(@"cellForRowAtIndexPath indexPath %@", indexPath);
我得到以下打印一次:
cellForRowAtIndexPath indexPath <NSIndexPath 0x836f1d0> 2 indexes [0, 0]
是,第一個單元格?
奇怪的是,當我嘗試顏色的細胞像下面的單元格保持白色也:
cell.backgroundColor = [UIColor redColor];
下面是對tableViewController(我的,不是斯坦福的)頭。請注意context屬性,這樣我就可以傳遞coreData managedObjectContext來執行我需要的提取。
#import <UIKit/UIKit.h>
#import "CoreDataTableViewController.h"
@interface CDST02TableViewController : CoreDataTableViewController
@property (strong, nonatomic) NSManagedObjectContext *context;
@end
請發佈其餘的UITableViewCell委託方法 –
@ ing0我認爲沒有其他人,因爲我正在使用斯坦福大學的CoreD ataTableViewController。我的TableViewController是它的一個子類,所以我只實現了cellForRowAtIndexPath。其餘的是CDTVC的代碼。 – Mrwolfy
「頁面」對象可能有問題。這是一個'NSObject'嗎?它是否正確記錄'page.name'? –