Ios 试图设置UILabel的文本时,UIImage将消失
我正在使用以下代码初始化UITableView中的文本标签:Ios 试图设置UILabel的文本时,UIImage将消失,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在使用以下代码初始化UITableView中的文本标签: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row]; UITableViewCell *cell = [tableView deq
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell.textLabel setText: [menuTitlesEN objectAtIndex:indexPath.row]];
return cell;
}
我的表格由四个静态单元格组成,每个单元格包含一个UIImageView和一个UILabel
未调用setText
时,我的表看起来应该是:
但是调用setText
会导致UIImage
s不知何故消失:
我对iOS开发非常陌生。有人能给我指引正确的方向吗?试试这样可能会有帮助
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.imageView.image = [UIImage imageNamed:@"Image name that you have"];
[cell.textLabel setText: [menuTitlesEN objectAtIndex:indexPath.row]];
return cell;
}
由于您正在覆盖默认单元格(
cell.textLabel
)的UILabel
,因此非常感谢。因此,最好将标记属性指定给UITableViewCell
中的自定义UILabel
,然后为其指定文本
尝试类似的方法:
UILabel *lbl = (UILabel*)[cell viewWithTag:identityOfUILabel];
[lbl setText: [menuTitlesEN objectAtIndex:indexPath.row]];
单元格最终的外观如何与第一张图像中的相同?我有一种感觉,因为在您的源代码中,您定义的NSString
CellIdentifier
的名称以蓝色突出显示,我只能假设它恰好与您在.h文件中为图像图标使用的名称相同。@SantaClaus当我注意到对setText
的调用时。这是有效的,但您能描述一下有什么不同吗?访问cell.textlab
是否会处理嵌入单元格中的任何视图,而是创建一个简单的UILabel
?默认情况下,单元格有一组样式,您可以查看这些样式。默认情况下,UITableViewCell
在单元格中有一个标题(UILabel
)和UIImageView
。如果您尝试使用这些视图,则嵌入的视图将与这些视图重叠。您需要自己创建自定义单元格,或者只需使用默认的UITableViewCell视图