开发者

Custom cell created with IB is hidden after calling reloadSections:withRowAnimation:

so I followed this guide ("The Technique for Static Row Content") to create my own custom UITableViewCell-s that would contain one image.

The following code is excerpt from my tableView:cellForRowAtIndexPath: method:

UIImageView *imageView = (UIImageView *)[imageViewCell viewWithTag:1];
imageView.image = [UIImage imageNamed:imageName];
cell = imageViewCell;
NSLog(@"%@", cell);
...
return cell;

imageViewCell refers to my custom cell created in interface builder. As you can see I'm trying to change image each time.

Everything works fine, but if I use reloadSections:withRowAnimation: on the UITableView, this cell disappears.

Here's console output:

<UITableViewCell: 0x9c68fe0; frame = (0 0; 302 215); autoresize = RM+BM; layer = <CALayer: 0x4b88110>>
<UITableViewCell: 0x9c68fe0; frame = (0 120; 320 102); autoresize = W; layer = <CALayer: 0x4b88110>>
<UITableViewCell: 0x9c68fe0; frame = (-320 120; 320 102); alpha = 0; autoresize = W; layer = <CALayer: 0x4b88110>>
<UITableViewCell: 0x9c68fe0; frame = (-320 121; 320 105); alpha = 0; autoresize = W; layer = <CALayer: 0x4b88110>>
<UITableViewCell: 0x9c68fe0开发者_如何学Go; frame = (-320 120; 320 111); alpha = 0; autoresize = W; layer = <CALayer: 0x4b88110>>

So as you can see it's frame and alpha is changed to weird values and stays like that.

That makes sense, because I'm not initializing it each time again, it's initialized only once after waking up from nib.

How do I reset its attributes to make it visible again? I found method called prepareForReuse, but that didn't work. I need something that would reset alpha and frame to make it appear again.


Solution with loading nib each time

To be more clear about my first approach: I created the table view cell in the nib of view controller. I set up an outlet, so I could use it in tableView:cellForRowAtIndexPath:.

Since the cell's attributes were messed up after animation I figured that recreating that cell would definitely help. The problem was my nib was loaded only once and I (still) don't know how to do something like reinitialization on a view that was initialized by nib file.

So I decided to create a new nib file and load it each time. It's not exactly what I was looking for, but it works. Here's what the code looks like, it's very simple:

[[NSBundle mainBundle] loadNibNamed:@"TestTableCell" owner:self options:nil];
UIImageView *imageView = (UIImageView *)[imageViewCell viewWithTag:1];
imageView.image = [UIImage imageNamed:imageName];
cell = imageViewCell;
imageViewCell = nil; // imageViewCell is still an outlet and setting
                     // it to nil makes the nib load it again the next
                     // time - so I'm sure I'll get a new instance.
cell.selectionStyle = UITableViewCellSelectionStyleNone;


Using reloadSections:withRowAnimation: with UITableViewRowAnimationNone actually solves the problem for me, and it still shows animation on adding/deleting cells from section.

I still believe, that this is a bug in UIKit.


The cell is disappearing because something is changing its frame such that it eventually gets drawn off screen.

If the size of the image changes, the image view itself is set to autoresize and the tableview cell's content view does as well, that might cause the cell's frames to migrate depending on the order of which the images load.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜