[英]Custom UItableViewCell label is overlapping
使用UITableView
并发现cells
无法正确出队。我使用storyboard
UIlabel
创建了原型单元,并在单元UIlabel
了UIlabel
。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
textLabel.font=[UIFont fontWithName:@"MyUnderwood" size:16];
textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
return cell;
}
这是UITableView
的图像,当我滚动TableView
时得到
您没有在CellForRowAtIndexPath
使用自定义单元格。 只需将UITableViewCell
放入队列中,代码看起来就很好了,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell"; //needs to be the identifier you defined in story boards
YourCustomCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[YourCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
textLabel.font=[UIFont fontWithName:@"MyUnderwood" size:16];
textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
return cell;
}
然后,您需要创建UITableViewCell的子类,并将其命名为YourCustomCell并链接所有IBOutlet
对象(例如标签等)。将这行代码放在.h文件#import YourCustomCell.h
顶部,并只需访问该单元格(如cell.textLabel
所有属性cell.textLabel
而不是创建标签并将其强制转换为带有标签的视图。
这可能只是一个小错误。 尝试使用其他标签值,或验证原型单元中正在使用的值。
我通过使用“ nil”代替cellIdentifier解决了此问题
例如:代替
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
用这个
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];
它对我有用
您只需要在情节提要中标记标签的“清除图形上下文”属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.