iOS model值修改产生的问题

开发问题笔记汇总

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    //1.根据model赋值
    cell.textLabel.text = self.dog.food;
    //2.修改数据model值
    if ([self.dog.food containsString:@"."]) {
        NSArray *arr = [self.dog.food componentsSeparatedByString:@"."];
        if (arr.count > 1) {
            self.dog.food = arr[1];
        }
    }
    //3.根据新值进行赋值
    cell.detailTextLabel.text = self.dog.food;

    return cell;
}

分析:
这种逻辑处理,第一次展示没问题。当刷新tableView且没有重新获取数据时,由于model的值已被修改,第二次展示的值并非我们想要的。
解决办法:
用一个新的字符串来接受model的值,以及修改后的值,而不要直接修改model。

你可能感兴趣的:(iOS model值修改产生的问题)