뉴스 피드 역할을하는 tableview가 있습니다. 셀은 뉴스 피드 항목의 배열에서 채워집니다. 서버에서 JSON을 가져 와서 해당 입력에서 뉴스 피드 항목을 만들고이를 내 뉴스 피드 배열에 첨부합니다. 뉴스 피드 항목에는 제목, 설명 및 이미지 끈 문자열이 포함됩니다.swift 3 iOS tableview 데이터 소스 메모리
은시 :
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 데이터로 가득 얻을.
하지만 내 모든 뉴스 피드 항목을 배열 안에 보관하므로 메모리 사용량이 높고 각 추가 뉴스 피드에 대한 gehts가 더 높습니다. 트위터처럼 끝없이 스크롤되도록 노력하겠습니다. 경험 많은 개발자가이 메모리 문제를 해결하는 방법에 대해 궁금합니다.
의 코드 내가 누락 된 코드를 추가 한 –
로 cellForRowAt 방법을 추가하십시오! –
페이지 매김을 구현하는 것이 좋습니다. 어떤 종류의 페이지 매김? 페이지 인덱스 번호가 오래된 것 같습니다. 그래서 트위터에 대해 생각해 보았습니다. 하단에 도달하면 새로운 트윗을로드하지만로드하지 않고 다시 스크롤 할 수 있습니다. 즉, 데이터를 어딘가에 저장하고 메모리에있는 모든 트윗을 유지하는 것이 내가 추측하는 응용 프로그램을 충돌. – Ccr