UITableView不重用自定义单元格

时间:2016-01-10 18:35:52

标签: ios objective-c uitableview

我的UITableView会在滚动出视图时不断重绘自定义单元格。我正在使用子类UITableViewCell。我认为问题在于我在layoutSubviews中调用我的方法。这是tableView的代码:

- (void)setupTableView
{
  [self.facitlityTableView setDataSource:self];
  [self.facitlityTableView setDelegate:self];
  [self.facitlityTableView registerClass:[FacilityTableViewCell class] forCellReuseIdentifier:@"Cell"];
  [self.facitlityTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
  return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return 90;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";

  FacilityTableViewCell *cell = (FacilityTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = (FacilityTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  }

  return cell;
}

以下是FacilityTableViewCell

的代码
- (void)layoutSubviews
{
  [super layoutSubviews];
  [self setupLogo];
  [self setupCompanyName];
  [self isOpen];
  [self setupProvidesList];
}

- (void)setupLogo
{
  self.letter = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.height/1.25, self.frame.size.height-2)];
  [self.letter setBackgroundColor:[UIColor redColor]];
  [self.letter setText:@"K"];
  [self.letter setTextAlignment:NSTextAlignmentCenter];
  [self.letter setFont:[UIFont systemFontOfSize:40]];
  [self.letter setTextColor:[UIColor whiteColor]];
  [self.letter setAlpha:0.7];
  [self addSubview:self.letter];
}

- (void)setupCompanyName
{
  self.companyName = [[UILabel alloc] initWithFrame:CGRectMake(self.letter.frame.size.width + kUIPadding, kUIPadding, 0, 0)];
  [self.companyName setText:@"Company Name"];
  [self.companyName setFont:[UIFont boldSystemFontOfSize:16.0f]];
  [self.companyName sizeToFit];
  [self.companyName setTextColor:[UIColor blackColor]];
  [self addSubview:self.companyName];
}

- (void)setupProvidesList
{
  self.providesList = [[UILabel alloc] initWithFrame:CGRectMake(self.letter.frame.size.width + kUIPadding, self.companyName.frame.size.height + kUIPadding, self.frame.size.width - (self.letter.frame.size.width + kUIPadding), 40)];
  [self.providesList setNumberOfLines:2];
  [self.providesList setFont:[UIFont systemFontOfSize:14]];
  [self.providesList setText:@"Food, Hot Drinks, Food, Hot Drinks, Food, Hot Drinks, Food, Hot Drinks, Food, Hot Drinks"];
  [self addSubview:self.providesList];
}

- (void)isOpen
{
  self.open = [[UIView alloc] initWithFrame:CGRectMake(self.companyName.frame.size.width+kUIPadding,kUIPadding,self.companyName.frame.size.height,self.companyName.frame.size.height)];
  self.open.alpha = 0.5;
  self.open.layer.cornerRadius = self.companyName.frame.size.height/2;
  self.open.backgroundColor = [UIColor blueColor];
  [self addSubview:self.open];
}

- (void)dealloc
{
  [self.letter removeFromSuperview];
  [self.companyName removeFromSuperview];
  [self.providesList removeFromSuperview];
  [self.open removeFromSuperview];
}

0 个答案:

没有答案