我正在使用界面生成器創建自己的自定義UITableViewCell。我支持iOS 5 & iOS 6,但我不想使用Storyboard。請不要建議故事板。我堅持使用Interface Builder並以編程方式編寫。自定義UITableViewCell不加載
我創建了一個UITableViewCell的子類。這裏的.h文件:
@interface CategoryCell : UITableViewCell
{
__weak IBOutlet UIImageView *image;
__weak IBOutlet UILabel *name;
__weak IBOutlet UILabel *distance;
__weak IBOutlet UILabel *number;
__weak IBOutlet UIImageView *rating;
}
@property (nonatomic, weak) IBOutlet UIImageView *image;
@property (nonatomic, weak) IBOutlet UILabel *name;
@property (nonatomic, weak) IBOutlet UILabel *distance;
@property (nonatomic, weak) IBOutlet UILabel *number;
@property (nonatomic, weak) IBOutlet UIImageView *rating;
@end
的XIB文件類型的UIViewController並具有類型CategoryCell的景色。我必須連接網點。
的問題
dequeueResuableCellWithIdentifier
不調用自定義單元格。下面是我有:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CategoryCell";
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CategoryCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
.....
}
return cell
}
當我代替loadBundle
符合:cell = [[CategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
,它的工作。但是筆尖不加載。所以一個單元格加載,但不是我自己的單元格,所以我不能設置我想要的標籤和圖像。當添加常規加載捆綁線(如上面的示例所示)並斷開自定義單元格的方法時,它不會被調用。此外,我得到的是一個全白屏幕,覆蓋了模擬器中的整個iPhone屏幕。
這是怎麼回事?我在這裏做錯了什麼?當我嘗試將網點設置爲strong
(我知道我不應該這樣做)時,它也不起作用。
編輯:
UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"CategoryCell" bundle:nil];
cell = (CategoryCell *)temporaryController.view;
它是什麼,我已經做了錯誤的方法,一個NSBundle:
我用更換一個NSBundle線固定呢?據說這應該是「更容易」的方式來做到這一點。
你有沒有放置一個斷點,看看topLevelObjects的值和單元格?這將是有用的信息,看看他們是否爲零。 –
是的 - 它不是零 – darksky