iOS object-c 小技巧

@高效码农  May 12, 2020

一、iOS12版本 使用reloadRowsAtIndexPaths方法 刷新cell 界面抖动/跳动

解决方法:

__strong typeof(self) strongSelf = self;
        [UIView performWithoutAnimation:^{
            CGPoint loc = self.tableView.contentOffset;
            [self.tableView reloadRowsAtIndexPaths:@[self.indexPath] withRowAnimation:UITableViewRowAnimationFade];
            strongSelf.tableView.contentOffset = loc;
        }];

二、A界面跳转到B界面,操作后返回A界面刷新单个组件(view,cell)

解决方案:
在B界面重写返回按钮事件:

- (void)backBtnClicked{
    [self.view endEditing:YES];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];


    UIViewController *ctl = self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2];
    if ([ctl isKindOfClass:[QAListViewController class]]) {

       QAListViewController *QAVC = (QAListViewController *)ctl;
        QAVC.questionID = self.questionID;
        QAVC.is_upate = YES;
        self.answerModel.like_type = [NSString stringWithFormat:@"%ld", (long)self.answerModel.good_bad];
        QAVC.answerModel = self.answerModel;
        [self.navigationController popToViewController:QAVC animated:YES];

    }else{

         [self.navigationController popViewControllerAnimated:YES];
    }
    
}

在A界面定义变量:是否需要更新is_update,以及更新的内容
在A界面的生命周期viewWillAppear中判断:

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.is_upate) {
        [self.newsDataArray replaceObjectAtIndex:self.cellIndexPath.row withObject:self.answerModel];
        
        __strong typeof(self) strongSelf = self;
        [UIView performWithoutAnimation:^{
            CGPoint loc = self.tableView.contentOffset;
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:self.cellIndexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
            strongSelf.tableView.contentOffset = loc;
        }];
        self.is_upate = NO;
    }
}

三、更新某个section,cell数量不会改变

解决方案:用[self.tableView reloadData];;大家又更好的方法欢迎留言交流



评论已关闭