Reputation: 337
I have this in my controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PostCell";
PostCell *postCell = (PostCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
Post *post = self.displayPosts[indexPath.row];
[postCell setPost:post];
return postCell;
}
With [postCell setPost:post];
I send my custom cell model that its going to use.
- (void)setPost:(Post *)newPost
{
if (_post != newPost) {
_post = newPost;
}
[self setNeedsDisplay];
}
Now this is version that I have but I want to change it. Now I have [self setNeedsDisplay]
which calls drawRect
- I want to use subviews
and dunno how to initiate that.
Where do I add subviews
and if using IBOutlets
where do I set subviews values (imageviews
image, labels
text etc...) based on that _post ??
Or if easier to point me how to add buttons
to my cell with drawRect and how to detect touch
on images and buttons inside drawRect
? That way I wont need to change current version.
Upvotes: 0
Views: 399
Reputation: 337
- (void)setPost:(Post *)newPost
{
if (_post != newPost) {
_post = newPost;
}
[self layoutSubviews];
[self refreshContent];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.titleLabel == nil) {
self.titleLabel = [[UILabel alloc] init];
[self addSubview:self.titleLabel];
}
if (self.imageView == nil) {
self.imageView = [[UIImageView alloc] init];
[self addSubview:self.imageView];
}
// here I also set frames of label and imageview
}
- (void)refreshContent
{
[self.titleLabel setText:_post.title];
[self.imageView setImage:self.post.image];
}
This is the way I've done it and it works great without any lags in UITableView
.
Upvotes: 1
Reputation: 2768
- (void)setPost:(Post *)newPost
{
if (_post != newPost) {
_post = newPost;
}
//check subviews exist and set value by newPost, if need no view, add subview, if need value, set new value
//Following code is just sample.
if(!self.imageView){
self.imageView = [[UIImageView alloc] init];
[self.contentView addSubview:self.imageView];
}
if(newImage){
self.imageView = newImage;
}
[self setNeedsDisplay];
}
Upvotes: 0