简体   繁体   English

UITableViewCell分隔线在滚动中消失

[英]UITableViewCell separator line disappears on scroll

I'm trying to have a separator cell with a custom image. 我正在尝试使用自定义图像分隔单元。 I did try something like that: 我确实尝试过这样的事情:

In my cellForRowAtIndexPath: 在我的cellForRowAtIndexPath中:

NSString *cellIdentifier = [NSString stringWithFormat:@"identifier"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}

cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:19];
cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor colorWithRed:128/255.0f green:129/255.0f blue:132/255.0f alpha:1.0f];
cell.backgroundColor = [UIColor whiteColor];

UIImageView *imagView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"reaL.png"]];
imagView.frame = CGRectMake(0, cellHeight, cellWidth, 1);
[cell.contentView addSubview:imagView];

switch (indexPath.row) {
    case 0:
        cell.imageView.image = [self imageWithImage:[UIImage imageNamed:@"img1.png"] scaledToSize:CGSizeMake(27, 27)];
        cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:@"route.png"] scaledToSize:CGSizeMake(27, 27)];
        break;
    case 1:
        cell.imageView.image = [self imageWithImage:[UIImage imageNamed:@"img.png"] scaledToSize:CGSizeMake(27, 27)];
        cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:@"money.png"] scaledToSize:CGSizeMake(27, 27)];
        break;
    case 2:
        cell.imageView.image = [self imageWithImage:[UIImage imageNamed:@"auto.png"] scaledToSize:CGSizeMake(27, 27)];
        cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:@"cars.png"] scaledToSize:CGSizeMake(27, 27)];
        break;
    case 3:
        cell.imageView.image = [self imageWithImage:[UIImage imageNamed:@"impostazioni.png"] scaledToSize:CGSizeMake(27, 27)];
        cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:@"impostazioni.png"] scaledToSize:CGSizeMake(27, 27)];
       // cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        break;
    case 4:
        cell.imageView.image = [self imageWithImage:[UIImage imageNamed:@"info.png"] scaledToSize:CGSizeMake(27, 27)];
        cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:@"info.png"] scaledToSize:CGSizeMake(27, 27)];
        break;
    default:
        break;
}

return cell;

When I lunch the app everything is good, but when I scroll the the table, or when I select a cell the separator lines disappear. 当我在应用程序中吃午餐时,一切都很好,但是当我滚动表格或选择单元格时,分隔线就会消失。 How I can have a permanent custom line separator? 我如何拥有永久的自定义行分隔符?

You're not creating cells properly in the first place. 首先,您没有正确创建单元格。 Here's a few tips to try that might help with the issue you're running into. 这里有一些技巧可以帮助您解决所遇到的问题。

First off, since ios6, Apple now allows us to pre-register the classes we're using to dequeue cells so there is no more deed to do the !cell check. 首先,从ios6开始,Apple现在允许我们预先注册用于使单元出队的类,因此不再需要进行!cell检查。 In your view did load, you'll want to do something akin to: 在加载视图中,您将需要执行以下操作:

[self.tableView registerClass:[CustomTableViewCellClass class] forCellReuseIdentifier:customIdentifier];

This will allow you to dequeue the cell properly every time, as in: 这将使您每次都能正确地使单元出队,如:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:customIdentifier];

Next, and this is where I think you're running into issues. 接下来,这是我认为您遇到问题的地方。 You're creating an image view and adding it to the cell every time the cell is dequeued. 您正在创建一个图像视图,并在每次使该单元出队时将其添加到该单元中。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"reaL.png"]];
imageView.frame = CGRectMake(0, cellHeight, cellWidth, 1);
[cell.contentView addSubview:imageView];

You only need to do this the first time the cell is created otherwise you're going to be adding constantly UIImageViews to the top of the cell every time a cell is re-used. 您只需要在第一次创建单元格时执行此操作,否则每次重新使用单元格时,都需要在单元格顶部不断添加UIImageViews。 A better pattern for this situations would be to have a UIImageView* reference on the custom cell itself. 对于这种情况,更好的模式是在自定义单元格本身上具有UIImageView *引用。 When the cell is dequeue, set the image and frame then. 当单元出队时,然后设置图像和帧。 This way the UIImageView is only created once and added to the cell once. 这样,UIImageView仅创建一次并添加到单元一次。

Best of luck! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM