表视图单元重新加载第一个单元格内容

时间:2015-04-01 13:01:19

标签: ios objective-c uitableview

这就是我的表格填充方式:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CellNewsInfo *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {

        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    // Set up the cell
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    NSString *titleArticle=[[stories objectAtIndex: storyIndex] objectForKey: @"title"];
  titleArticle = [titleArticle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if (indexPath.row==0) {
        scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
        scr.tag = 1;
        scr.autoresizingMask=UIViewAutoresizingNone;
        [cell addSubview:scr];
        [self setupScrollView:scr];
        UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(120, 170, 80, 36)];
        [pgCtr setTag:12];
        pgCtr.backgroundColor = [UIColor clearColor];
        pgCtr.numberOfPages=5;
        pgCtr.tintColor=[UIColor redColor];
        pgCtr.pageIndicatorTintColor=[UIColor blueColor];
        self.pageControl.hidden=YES;
        pgCtr.currentPageIndicatorTintColor = [UIColor redColor];
        pgCtr.autoresizingMask=UIViewAutoresizingNone;
        [cell addSubview:pgCtr];
    }
    else{
    cell.title.text=titleArticle;
    cell.title.numberOfLines=2;

为什么当我滚动它时,第一个单元格正在重新加载?我只想在初学者那里只有一次滚动视图。

1 个答案:

答案 0 :(得分:3)

再次添加scrollview的原因是,一旦取消分配单元格,它们将被重用。

如果要在一个tableView中显示多个单元格类型,或者甚至使用两个不同的单元格标识符(取决于行是否为0),您应该考虑创建自己的自定义单元格。

CellNewsInfo *cell;
if (indexPath.row == 0) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"scrollCell" forIndexPath:indexPath];

    if ([cell viewWithTag:1]) {
        scr = [cell viewWithTag:1];
    }
    else {
        scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
        scr.tag = 1;
    }
    // continue customization here with scrollview
}
else {
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    // continue customization here without scroll view
}

return cell;