[英]Empty prototype cell for a UITableView
我有一個新項目,必須將原型單元拖到現有的表格視圖中。 然后我
然后在我的tableview委托中,當我得到委托回調時:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myCustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:11];
我看到nameLabel為零。 我已經仔細檢查過標簽,並且三重檢查了標簽和可重復使用的標識符,但是沒有運氣。 在情節提要中,我看到tableview具有pro type單元格作為其單元格,並且contentView顯示了我的標簽。 我想念什么?
您是否曾經在if (cell == nil)
分支中創建一個新單元if (cell == nil)
? 如果是,則在此處創建常規的UITAbleViewCell。 不要期望在那里有任何自定義標簽,因為您不會從任何nib文件或情節提要中加載它。
單元格對象是什么類型的? NSLog或在調試器中查看實際創建的類型。
您正在分配不包含自定義標簽的UITableViewCell。 如果使用Storyboard創建了一個View,則應使用以下方法分配該View。
您可以創建一個Utility方法來從NibName獲取Class實例。
+ (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass
{
if (nibName && objClass)
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName
owner:nil
options:nil];
for (id currentObject in objects )
{
if ([currentObject isKindOfClass:objClass])
return currentObject;
}
}
return nil;
}
在您的代碼中像這樣使用
CustomViewCell *cell = (CustomViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomViewCell"];
if (cell == nil)
{
cell = [Utility loadNibNamed:@"CustomViewCell" ofClass:[CustomViewCell class]];
}
cell.yourLabel.text = @"Dummy Text";
希望這會對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.