我有一个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.
更多推荐
发布评论