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

4
这是我的表格如何填充的:
- (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;

为什么我滚动时,第一个单元格会重新加载?我只想在开头时滚动视图一次。

将您的代码添加到 if (!cell) 中,然后尝试。 - Darshan Kunjadiya
1个回答

3

如果您的滚动视图被再次添加,则是因为一旦单元格被释放,它们就会被重用。

如果您要在一个tableView中显示多个单元格类型,甚至根据行号使用两个不同的单元格标识符,那么您应该考虑创建自己的定制单元格。

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;

你的想法有效,谢谢,但现在出现了另一个问题。当我滚动并回到顶部时,自动滚动不起作用,似乎有两个滚动视图。 - Adina Marin
在将您的滚动视图添加到单元格之前,请检查并查看它是否已经存在。检查单元格是否包含具有您所赋予的标记的子视图,如果是,则继续分配值,如果不是,则在分配值之前创建并添加它。 - c_rath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接