【发布时间】:2014-10-06 20:19:57
【问题描述】:
自定义 UITableViewCell 遇到了一个非常奇怪的问题。顺便说一句,我正在使用 UIViewController。所以,我在 Storyboard 中制作了单元格(如下图所示),并将它的类设置为我的自定义 UITableViewCell 类。然后我在自定义单元类中创建了所有 IBOutlets 和 IBActions。
我的 cellForRowAtIndexPath 方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
PostTableViewCell *cell = (PostTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setCellContentWithPost:[PostsArray objectAtIndex:indexPath.row]];
return cell;
}
我的自定义 UITableViewCell 类:
#import "PostTableViewCell.h"
@implementation PostTableViewCell
- (void)setCellContentWithPost:(SDPost*)post {
self.alpha = 0.f;
self.postTitleLabel.text = post.title;
[self.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:post.thumbnailURL] placeholderImage:nil options:SDWebImageHandleCookies];
[UIView animateWithDuration:0.35 animations:^{
self.alpha = 1.0f;
}];
}
-(void)awakeFromNib{
self.postTitleLabel.textColor = [UIColor whiteColor];
self.postTitleLabel.font = [UIFont fontWithName:@"Montserrat-Regular" size:16.5];
self.readingTimeView.backgroundColor = [UIColor colorWithRed:0.33 green:0.74 blue:0.15 alpha:1];
self.readingTimeView.layer.cornerRadius = 4;
self.readingTimeLabel.textColor = [UIColor whiteColor];
self.readingTimeLabel.font = [UIFont fontWithName:@"Montserrat-Bold" size:11.75];
self.commentsCountView.backgroundColor = [UIColor colorWithRed:0.74 green:0.19 blue:0.4 alpha:1];
self.commentsCountView.layer.cornerRadius = 4;
self.commentsCountLabel.textColor = [UIColor whiteColor];
self.commentsCountLabel.font = [UIFont fontWithName:@"Montserrat-Bold" size:11.75];
}
我尝试通过 UITableViewCell 的 initWithStyle 方法设置单元格的样式,但由于某种原因它从未被调用,所以我最终在 awakeFromNib 中执行此操作。 所以,问题是:我认为我做错了什么,因为正如你在这个 GIF (https://dl.dropboxusercontent.com/s/6crcjbmitr5fmk7/Untitled%20%281%29.gif?m=) 中看到的那样,当我滚动单元格时,它得到的心形按钮会自动打开/关闭。
你们中的任何人都可以帮我解决这个问题吗?非常感谢!
【问题讨论】:
-
点击心脏后是否更新数据源PostsArray?
-
检查
setCellContentWithPost中的代码。您必须重置heart button的图像 -
@jithinroy,不,我只是在更改心形按钮的图像。
-
当用户选择心脏(选中)或未选中时,您应该在 PostArray 中有一个 Bool(选中或未选中)值。这里是类似的example Table View Accessory
-
除非我错过了什么,否则我看不到任何设置心脏图像的代码。这正是您正在执行的代码,还是您在此处发布之前对其进行了修整?
标签: ios objective-c uitableview