处理重复数据和uitableviewcell中的受影响单元格

As you know, UITableView really helps developers and designers to display the data simply. The main component of UITableView is the cell (UITableViewCell). All of your data will be displayed in the table through the cells. A set of cells will form a table. You can read full documentation and implementation trough the official documentation on the Apple Developer site.

如您所知,UITableView确实可以帮助开发人员和设计人员简单地显示数据。 UITableView的主要组件是单元格(UITableViewCell)。 您的所有数据将通过单元格显示在表格中。 一组单元格将构成一个表格。 您可以通过Apple Developer网站上的官方文档阅读完整的文档和实现。

UITableView``` 

Personally, In the early days of my exploration with UITableView. I had some issues in using UITableView. I set one cell to “active” condition, but after scrolling down the table I found out that other cells were affected with ✅ emoji and constantly duplicated to other cells below.

就个人而言,在我探索UITableView的早期。 使用UITableView时遇到一些问题。 我将一个单元格设置为“活动”状态,但是向下滚动表格后,我发现其他单元格受到 ✅表情符号的影响 ,并不断复制到下面的其他单元格中。

OH DANG!
哦,老兄!

As a beginner, I didn’t know why it happened. I tried to ask my mentors at at the Apple Developer Academy, where I used to be enrolled not long ago. Later, we found out that Swift is using “dequeReusableCell” to save its memory use and boost its performance. What do we need to do if we want to display 1000 data? Instead of creating 1000 cells, we just need to create a little visible cell (5–6) and reuse those cells to display another data. That “reuse” thing also caused this to happen. The “active status” was duplicated to another cell after I scrolled down the table. I’ve already found further explanation about “reuse thing” problems. Read this before you continue — > Why we use dequeueReusableCellWithIdentifier and this.

作为一个初学者,我不知道为什么会这样。 我试图问我在Apple Developer Academy的导师,不久前我曾经在那儿入学。 后来,我们发现Swift正在使用“ dequeReusableCell”来节省其内存使用并提高其性能 。 如果要显示1000个数据,该怎么办? 无需创建1000个单元格,我们只需要创建一个可见的单元格(5-6),然后重复使用这些单元格即可显示其他数据。 这种“重复使用”的事情也导致了这种情况的发生。 向下滚动表格后,“活动状态”已复制到另一个单元格。 我已经找到了有关“重用物品”问题的进一步解释。 在继续之前,请先阅读本内容—> 为什么我们使用dequeueReusableCellWithIdentifier 和this

Based on what I’ve read, both of those articles suggest resetting the cell using prepareForReuse(). Just simply change the alpha, isEnabled condition, color, attribute, and the default value of your cell. Wow, it seemed super easy. But wait a minute! Take a look at what Apple recommends us not to do.

根据我的阅读,这两篇文章都建议使用prepareForReuse()重置单元格。 只需更改alpha,isEnabled条件,颜色,属性和单元格的默认值即可。 哇,看起来超级容易。 但是请稍等! 看一看苹果建议我们不要做什么。

Image for post
It will affect its performance they said 他们说,这将影响其性能
Whoa, so what must we do with our cells?
哇,那我们该怎么处理细胞呢?

公共职能救助 (Public function to the rescue)

After a long journey of digging the internet. I tried to do something with my cell Swift file. I made a simple public function to reset the cell view and call it from my view controller file. This way, we don’t need to reset first and set after. Just call that public function from ViewController.swift file and you can update your cell automatically.

经过漫长的互联网探索之旅。 我试图对我的单元Swift文件进行处理。 我做了一个简单的公共函数来重置单元格视图并从我的视图控制器文件中调用它。 这样,我们不需要先重置然后再设置。 只需从ViewController.swift文件中调用该公共函数,即可自动更新单元格。

Image for post
Simple solution to reset and update your cell
重置和更新单元的简单解决方案
Image for post
Make a public function and configure the view based on “isActive” condition
公开功能并根据“ isActive”条件配置视图
Image for post
Call the public function that we created before at TableCustomCell.swift from our ViewController.swift
从ViewController.swift调用我们在TableCustomCell.swift之前创建的公共函数

I have already implemented that prepareForReuse() solution and public function solution. So far, I don’t have any issues with my code, but sometimes I get new duplicated image errors when dealing with updating imageView in cell, but I think that’s another case.

我已经实现了prepareForReuse() 解决方案公共功能解决方案 。 到目前为止,我的代码没有任何问题,但是有时在处理单元格中的imageView更新时会出现新的重复图像错误,但是我认为这是另一种情况。

Thanks for your attention and wait for another story about “Dealing with imageView in Cell” soon!

感谢您的关注,并等待其他有关“在单元格中使用imageView处理”的故事!

翻译自: https://medium.com/swlh/dealing-with-duplicated-data-and-affected-cell-in-uitableviewcell-bcb18903873b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值