In custom Prototype Cell in UITableView, UITextField value replaces with other Prototype Cell

前端 未结 2 817
误落风尘
误落风尘 2021-01-06 18:24

I have taken one UITableView and inside that I have used 2 prototype cells, now in those prototype cells, i have taken 1 UITextField.<

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 18:52

    When you use dequeueReusableCellWithIdentifier, your cells will be reused. This means when the user scrolls the tableview, a cell which moves out of the screen will be reused to display the contents of a cell which is about to move onto the screen.

    Even though this helps in saving memory, the problem it causes is the that the cell needs to be prepared for display before it is loaded with the content of the new cell.

    In your case, it seems you need to maintain the values the user has entered in a textfield of a cell.

    So to fix your problem, if there are not that many cells in the tableview, simply stop reusing the cell(Use alloc-init/new for each cell instance). From your code it looks like you have less than 10 cells and this would be the quickest way to fix the issue.

    Else, if there are a large number of cells, whenever the user enters a value in the textfield of a cell, save it in an array. And fetch the value from from the array and set it to your textfield in the cellForRowAtIndexPath method.

提交回复
热议问题