iOS Swift自定义单元格功能(iOS Swift Custom Cell Function)

编程入门 行业动态 更新时间:2024-10-18 08:30:09
iOS Swift自定义单元格功能(iOS Swift Custom Cell Function)

我有一个TableView和自定义单元格。 在我的自定义单元格中,我有这个功能:

func URLRequest(bookId : String) { let headers = [ "Authorization": "Bearer", "Accept": "application/json" ] let url = "http://example.com/api/v1/books/\(bookId)/related" let URLRequest = NSMutableURLRequest(URL: NSURL(string: url)!) URLRequest.cachePolicy = NSURLRequestCachePolicy.ReturnCacheDataElseLoad UIApplication.sharedApplication().networkActivityIndicatorVisible = true Alamofire.request(.GET, url, headers:headers) .validate() .responseJSON { response in switch response.result { case .Success: if let jsonData = response.result.value { dispatch_async(dispatch_get_main_queue(), { print("Custom Cell") return }) self.tableRefresh() UIApplication.sharedApplication().networkActivityIndicatorVisible = false } case .Failure(let error): UIApplication.sharedApplication().networkActivityIndicatorVisible = false print(error) } } }

我在tableView中运行此函数:

if indexPath.row == 4 { let cell = tableView.dequeueReusableCellWithIdentifier("relatedCell", forIndexPath: indexPath) as! RelatedBookTableViewCell cell.URLRequest(self.bookID) return cell }

但问题是每次我滚动表视图时此请求(URLRequest)再次运行。 我想发送此请求一次,当视图加载时。

我的问题在哪里?

PS我尝试在自定义单元格中加载我的URLRequest

override init()

但是我无法将bookId值从tableview传递给自定义单元格并返回nil。

谢谢

I have a TableView and custom cell. in my custom cell i have this function:

func URLRequest(bookId : String) { let headers = [ "Authorization": "Bearer", "Accept": "application/json" ] let url = "http://example.com/api/v1/books/\(bookId)/related" let URLRequest = NSMutableURLRequest(URL: NSURL(string: url)!) URLRequest.cachePolicy = NSURLRequestCachePolicy.ReturnCacheDataElseLoad UIApplication.sharedApplication().networkActivityIndicatorVisible = true Alamofire.request(.GET, url, headers:headers) .validate() .responseJSON { response in switch response.result { case .Success: if let jsonData = response.result.value { dispatch_async(dispatch_get_main_queue(), { print("Custom Cell") return }) self.tableRefresh() UIApplication.sharedApplication().networkActivityIndicatorVisible = false } case .Failure(let error): UIApplication.sharedApplication().networkActivityIndicatorVisible = false print(error) } } }

and I run this function in my tableView:

if indexPath.row == 4 { let cell = tableView.dequeueReusableCellWithIdentifier("relatedCell", forIndexPath: indexPath) as! RelatedBookTableViewCell cell.URLRequest(self.bookID) return cell }

But the problem is every time that I scroll the table view this request(URLRequest) run again. I want to send this request once and when the view did load.

where is my problem?

P.S I try to load my URLRequest in custom cell with this

override init()

but I can't pass bookId value from tableview to custom cell and return nil.

thanks

最满意答案

您正面临的问题正在发生,因为表视图单元格被表视图重用 。 这意味着操作系统只在内存中保留少量单元格,但在滚动时,它会在视图中移动它们。

无论如何,有两种方法可以解决这个问题:

第一个涉及在表视图控制器中使用书信息(使用字典或数组),当您初始化单元格时,只需在单元格中设置属性即可。 首选

第二个是将当前bookId存储在单元格中,如果单元格当前正在显示该书籍ID,则返回。 不建议这样做。

请记住,单元格不是控制器,它应该只用于存储数据和修改视图。 所有逻辑都应该在表视图控制器中。

The issue you're facing is occurring because the table view cells get reused by the table view. That means that the OS only keeps few cells in memory, but when you are scrolling, it moves them in the view.

Anyways, there are two ways of solving this issue:

The first one involves having the book information in the table view controller (With a dictionary or an array), and when you're initializing the cell, just set the properties in the cell. Preferred.

The second one is storing the current bookId in the cell, and if the cell currently is displaying that book id, just return. This is not recommended.

Remember, the cell is not a controller, it should only be used to store data and modify the view. All the logic should be in the table view controller.

更多推荐

本文发布于:2023-08-07 05:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   单元格   功能   Swift   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!