cell.detailTextLabel.text is NULL

对着背影说爱祢 提交于 2019-12-08 04:50:26

问题


I do not understand why my cell.detailTextLabel.text is null.

static NSString *CellIdentifier = @"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyId];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    /*[[NSBundle mainBundle] loadNibNamed:@"CellTest2" owner:self options:nil];
    cell = mealPlanCell;*/
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    //self.mealPlanCell = nil;
}

cell.detailTextLabel.text = @"Test";

It will let me set the cell.textLabel.text to a value but not the detail one.


回答1:


I figured out the reason if you leave

if(cell == nil){...}

around the cell = [[[UITableViewCell alloc]...

then the cells will not re render and thus will not update the style and or details so if you remove the if() then this will allow you to re render each cell in the table view if you are changing style back and forth. This is not a good thing to remove though if you think about it cause then it is going to low the tables "speed" which probably wont be noticeable but it is something to consider looking into as a possible issue. This is not what I did to solve the problem personally cause I choose to take a different approach but it is something that worked when I tried.




回答2:


I had same problem: UITableViewCell initWithStyle:UITableViewCellStyleSubtitle is not working

For those that search and find this question... and have same problem as me, where you are using a prototype cell and the style is not defined correctly in the storyboard.




回答3:


If your rowHeight is not tall enough, the cell.detailTextLabel.text won't be visible even if the cell.textLabel.text is visible. A rowHeight of 22 seems to be the minimum with the default font.



来源:https://stackoverflow.com/questions/9792129/cell-detailtextlabel-text-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!