我为我的tableview创建了自定义单元格,我在cellForRowAtIndexPath上更新它们一切正常....但是当我触摸一个单元格时,布局会混乱,我认为这一切都会在我触摸过的牢房左侧。就像在第二张照片中一样。
知道会发生什么事吗?
以下是ViewController和Cell的代码:
StoriesViewController.h
#import <UIKit/UIKit.h>
#import "ReactViewController.h"
#import "StoriesCell.h"
#import "SDWebImage/UIImageView+WebCache.h"
@interface StoriesViewController : ReactViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) NSDictionary *downloadedObject;
@end
StoriesViewController.m
#import "StoriesViewController.h"
@interface StoriesViewController ()
@end
@implementation StoriesViewController
NSArray *stories;
- (void)viewDidLoad
{
[super viewDidLoad];
stories = [self.downloadedObject objectForKey:@"stories"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [stories count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"StoryPreview";
StoriesCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[StoriesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.titleLabel.text = [currentObject objectForKey:@"title"];
cell.txtLabel.text = [currentObject objectForKey:@"text"];
[cell.avatarImageView setImageWithURL:[NSURL URLWithString:[currentObject objectForKey:@"avatar"]]
placeholderImage:[UIImage imageNamed:@"placeholder"]
options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
return cell;
}
@end
StoriesCell.h
#import <UIKit/UIKit.h>
@interface StoriesCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *authorLabel;
@property (weak, nonatomic) IBOutlet UILabel *txtLabel;
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
@end
StoriesCell.m
#import "StoriesCell.h"
@implementation StoriesCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (void)awakeFromNib
{ }
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
@end
布局在故事板上设置,如下图所示:storyboard