ios开发的那些坑

iOS-cell复用引起的分割线问题

2018-07-02  本文已影响71人  fly大梦想家
cell复用引起的问题.gif

首次进入界面的时候我的所有分割线都在,一滚动复用,复用过的cell上的线就没显示了(其实是被遮挡了),当时写的时候为了以防细线被遮挡,刻意写在了最后面,以保证细线在最上方

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HISTORY"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HISTORY"];
        }
        cell.backgroundColor = [CDCommon colorForKey:@"whiteBackgroundColor"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        for (UIView *subView in cell.contentView.subviews) {
            [subView removeFromSuperview];
        }
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 15, 15)];
        imageView.image = [CDCommon loadAppImage:@"searchTimeIcon"];
        [cell.contentView addSubview:imageView];
        
        UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 0, LAYOUT_SCREENSIZE_P.width - 45 * 2, 55)];
        textLabel.textColor = [CDCommon colorForKey:@"searchHistoryTextColor"];
        if (_searchHistories.count == 0) {
            return cell;
        }
        textLabel.text = _searchHistories[indexPath.row];
        textLabel.font = FONT_SIZE_SEARCH_HISTORY_TITLE;
        [cell.contentView addSubview:textLabel];
        
        UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        deleteButton.tag = indexPath.row;
        deleteButton.frame = CGRectMake(LAYOUT_SCREENSIZE_P.width - LAYOUT_SIZESCALE_PORTRAIT(120, 120).width, 13, 30, 30);
        [cell.contentView addSubview:deleteButton];
        [deleteButton setImage:[CDCommon loadAppImage:@"searchCloseButton"] forState:UIControlStateNormal];
        [deleteButton addTarget:self action:@selector(deleteOneHistory:) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 55 , LAYOUT_SCREENSIZE_P.width, LAYOUT_LINE_HEIGHT)];
        lineView.backgroundColor = [CDCommon colorForKey:@"searchHistorySeperateLineColor"];
        [cell.contentView addSubview:lineView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

为什么一复用就被遮挡了呢?难道cell复用的时候UI布局的顺序不是按照自己代码的顺序来执行的?

其实这个写法本来就是有问题的,我只是就这个问题思考了一下
(原本的问题是cell的高度是55,当时不知道为啥给线的Y设置的55,可能因为线比较细,所以最开始都是可以显示的,最后我把线的Y设置为55 - 线的高度)

上一篇 下一篇

猜你喜欢

热点阅读