我使用带有原型单元的故事板,如下所示:
之后我添加了动态的子视图计数:
问题有时在 uitableviewcell 的一半上出现白屏然后滚动。
并且 cell==nil 没有被调用。
或者只是解释如何将子视图的动态计数添加到 uitableviewcell。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifer;
cellIdentifer = @"PhotoWorkCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
if (cell == nil){
NSLog(@"is nil");
}
[self addSubviews:cell atIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];//just setting images and text to views
return cell;
}
-(void)addSubviews:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{
PhotoWork *work = [self.tableData objectAtIndex:indexPath.row];
PhotoWorkCell *workCell = (PhotoWorkCell*)cell;
for(UIView *view in workCell.contentView.subviews){
if ([view isKindOfClass:[CustomPoints class]])
[view removeFromSuperview];
}
for(int i=0; i <[work.pointsArray count];i++)
{
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"CustomPoints" owner:self options:nil];
CustomPoints *customPoints = [subviewArray objectAtIndex:0];
customPoints.frame = CGRectMake(10, 135+i*customPoints.frame.size.height, customPoints.frame.size.width, customPoints.frame.size.height);
customPoints.label= @"test";
workCell.photoButton.tag = indexPath.row;
[workCell.photoButton addTarget:self action:@selector(onHistory:) forControlEvents:UIControlEventTouchDown];
[workCell.contentView addSubview:customPoints];
}
}
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
PhotoWork *work = [self.tableData objectAtIndex:indexPath.row];
return 135+[work.pointsArray count]*57;
}