Table view cell only masks correctly after scrolling off and then back on

后端 未结 6 1609
無奈伤痛
無奈伤痛 2021-02-04 06:08

I\'m modifying a table view cell during a call to tableView:cellForIndexPath, but the modifications do not appear until the cell is scrolled off and then back on. I

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 06:59

    Approach - 1 reload cell in viewWillAppear with delay mathod
    
    func viewWillAppear(_ animated: Bool)
    { 
       super.viewWillAppear(animated) 
       DispatchQueue.main.asyncAfter(deadline: .now() + 0.10, execute: {
            self.tableView.reloadData() }) 
    }
    
    Approach - 2 Draw in GCD
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TheCorrectCellIdentifier" forIndexPath:indexPath];
    
    dispatch_async(dispatch_get_main_queue(), ^{
    
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight                                                         cornerRadii:CGSizeMake(4., 4.)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = cell.bounds;
        maskLayer.path = maskPath.CGPath;
        cell.layer.masksToBounds = YES;
        cell.layer.mask = maskLayer;
    
    });
        return cell;
    }
    

提交回复
热议问题