Weird animations when editing custom cell?

瘦欲@ 提交于 2020-03-04 21:31:12

问题


I created a UITableViewCell in Xcode 4 using IB. But for some weird reason when I trigger my edit button, the cell acts really weird and doesn't have the same animations as a normal table view cell. I don't know what is happening, I tried implementing -(void)setEditing:(BOOL)editing animated:(BOOL)animated in my custom cell class but still nothing works.

UPDATE: My ViewController that I am using the cell in, has this code under cellForAtIndex to display the cell.

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
    for (id currentObject in topLevelObjects) 
    {
        if ([currentObject isKindOfClass:[UITableViewCell class]]) 
        {
            cell = (CustomCell *) currentObject;
            break;
        }
        return cell;
    }
}

Normal view:

When tapping edit button:

And even when swiping it covers the text when it's supposed to move it aside:


回答1:


First suggestion; don't use IB for custom UITableViewCells.

Next; make sure you're adding any custom views to the UITableViewCell's contentView... not just the cell itself. When the editing portions are shown (like the delete button) the UITableViewCell's contentView will shrink automatically. If you have the correct UIViewAutoresizing mask on the UILabel / whatever you've added to the contentView, it will get resized / moved correctly.

Edit, to answer your question in comments:

The same way you would any custom view. You could make it a class on it's own, or, depending on your needs, construct it ad-hoc. A class is usually best so you can reference any of your custom things you've added to the cell and reuse cells / etc.

But, here's is an example of constructing one ad-hoc.

UIImageView * myCheckBox = ...
UILabel * myLabel = ...
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
[cell.contentView addSubview:myCheckBox];
[cell.contentView addSubview:myLabel];
self.customCell = cell;

Edit #2

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.textLabel.frame = CGRectMake(50, self.textLabel.frame.origin.y, self.contentView.frame.size.width - 50, self.textLabel.frame.size.height);
}


来源:https://stackoverflow.com/questions/8825522/weird-animations-when-editing-custom-cell

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