---
title: "ios UITableView 的 UITableViewCell 的重用机制不对造成内容重复读取"
author: "BlueSkator"
date: "2014-12-16T18:51:05Z"
tags: ["iOS", "重用"]
categories: ["移动开发"]
slug: "ios-uitableview-uitableviewcell"
draft: false
lastmod: "2014-12-16T18:51:05Z"
id: 2166843
url: "https://blueskator.iteye.com/blog/2166843"
---

```
NSString *reusedIdentifer = @"recipeDosageCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reusedIdentifer];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedIdentifer];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
```

 1.定义一个重用的cell，如果在外面给他的内容复制，当重复刷新就会重复显示cell的内容，之所以会造成这种结果，是在reloadData的时候，会重新调用cellForRowAtTableView ，然后找可以重用的cell，如果找到了就会用那个重用的cell，并且再次给他的内容赋值，结果就会造成重复显示。

 

解决方法，首先cell初始化时定义他的subview， 然后在外层给他的subview进行赋值。

 

2.如果一个tableview 中有多种cell ，则需要定义多个cell，然后进行重用