奇怪的自定义UITableViewCell行为

时间:2014-08-13 12:52:40

标签: ios objective-c uitableview

我在自定义UITableViewCell时遇到了一个非常奇怪的问题。顺便说一句,我正在使用UIViewController。所以,我在Storyboard中制作了单元格(如下图所示),并将它的类设置为我的自定义UITableViewCell类。然后我在自定义单元类中创建了所有IBOutlets和IBActions。 enter image description here

我的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=)中看到,当我滚动单元格时,它会自动打开/关闭心形按钮。

你们中的任何人都可以帮我解决这个问题吗?非常感谢!

3 个答案:

答案 0 :(得分:1)

每次显示单元格时,它都会从PostsArray数组中获取值。因此,当您单击心脏时,您应该更新数组中的相应对象。

答案 1 :(得分:1)

之所以会发生这种情况,是因为你的所有细胞都有相同的CellIdentifier,在这种情况下,如果你非常小心你如何处理“心脏”,那就很好了。元件。

  1. 您应该有办法确定单元格上的哪些元素已被"喜欢"。我想你需要知道用户按下心形按钮的元素。
  2. 初始化/重复使用您的手机时,您需要确保心形按钮已正确设置为"红色/开启"或"白/关"。
  3. 例如:

        - (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 setHeartState:[[AnArray objectAtIndex:indexPath.row] hasBeenLikedByUser]]
    
    
            [cell setCellContentWithPost:[PostsArray objectAtIndex:indexPath.row]];
    
            return cell;
        }
    

    这只是一个快速草案。

    希望有所帮助

答案 2 :(得分:0)

我设法通过检查是否应该填充炉灶来解决这个问题。我使用NSUserDefaults存储了帖子ID,并在setCellContentWithPost中添加了一个if语句,用于检查该单元格的帖子是否被收藏。

感谢有兴趣的人!

相关问题