У меня есть табличное представление, которое действует как новостная лента. Ячейки заполняются из массива элементов новостей. Я получаю JSON с сервера, создаю элементы новостей из этого ввода и присоединяю их к своему массиву лент новостей. элемент новостей содержит заголовок, описание и строку imageurl.swift 3 iOS tableview datasource memory
В:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "ImageFeedItemTableViewCell1", for: indexPath) as! ImageFeedItemTableViewCell
var item = self.feed!.items[indexPath.row]
if (item.messageType == 1){
cell = tableView.dequeueReusableCell(withIdentifier: "ImageFeedItemTableViewCell1", for: indexPath) as! ImageFeedItemTableViewCell
cell.title.text = item.title
cell.description.text = item.contentText
if (item.imageURL as URL == URL(string: "noPicture")!)
{
cell.picture.image = UIImage(named:"empty")
}
else{
if (item.cachedImage == UIImage(named:"default-placeholder")){
let request = URLRequest(url: item.imageURL as URL)
cell.picture.image = item.cachedImage
cell.dataTask = self.urlSession.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
OperationQueue.main.addOperation({() -> Void in
if error == nil && data != nil {
let image = UIImage(data: data!)
if (image != nil){
self.feed!.items[indexPath.row].cachedImage = image!
}
cell.picture.image = image
}
})
})
cell.dataTask?.resume()
}else
{
cell.picture.image = item.cachedImage
}
}
}
клетки из строк заполняются с моими данными newsfeeditem.
Но так как я сохраняю все свои новостные элементы внутри массива, использование памяти высока и выше для каждого дополнительного newsfeeditem. Я хочу, чтобы он работал с бесконечной прокруткой, как твиттер, поэтому мне интересно, как опытные разработчики решают эту проблему с памятью.
пожалуйста, добавьте метод cellForRowAt как фрагмент кода –
я добавил недостающий код! –
pagination - хороший способ реализовать, если есть много фидов – Ccr