2017-02-11 4 views
0

UITableViewCell의 기능 이미지 캐시를 만드는 데 큰 도움이되었습니다. cellForRowAtIndex입니다. 죄송합니다. 아래 코드으로 하나 이상의 이미지 만 반복해서 표시됩니다. 나는 cellForRowAtIndexPath이 for 루프와 같았고, 각각 row에 대해 다시 실행되었다는 인상을 받았다. 따라서 왜 하나의 이미지 만 표시되는지 궁금합니다.UITableViewCell에 대한 이미지 캐시를 만들었지 만 하나의 이미지 만 표시됩니다.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantcell") as? RestaurantTableCell 
    var oneRestaurant: Restaurant = tablerestaurantarray[indexPath.row] 

    if let cachedVersion = cache.object(forKey: "image") { 
     oneRestaurant = cachedVersion 
    } else { 
     cache.setObject(oneRestaurant, forKey: "image") 
    } 

    cell?.picture?.image = oneRestaurant.value(forKey: "image") as! UIImage? 

    let restaurant = restaurantArray[indexPath.row] 
    cell?.name?.text = restaurant.value(forKey: "Name") as? String 

    return cell! 
} 

업데이트 2 :

Results from the added breakpoint

+0

세포는 얼마나 큽니까? 스토리 보드의 기본 이미지는 무엇입니까? 내가 이해하는 것으로부터, 당신은 클래스'레스토랑'에서 이미지를 얻습니다. 그 수업을 게시 할 수 있습니까? –

+0

간단한 "var image = UIimage?" 수업을 위해서. – Jon

답변

0

당신은 다른 개체에 대해 동일한 NSCache 키 ("image")를 사용합니다. 따라서 첫 번째 Restaurant 개체 만 캐시에 저장됩니다. 다른 모든 셀의 경우 키 "image"에 대해 캐시 된 개체를 찾고 이전에 저장된 Restaurant 개체를 다시 가져옵니다.

다른 Restaurant 개체를 캐싱 할 때 다른 키를 사용해야합니다.

let key = "restaurant \(indexPath)" 

if let cachedVersion = cache.object(forKey: key) { 
    oneRestaurant = cachedVersion 
} else { 
    cache.setObject(oneRestaurant, forKey: key) 
} 

왜 레스토랑 개체를 캐싱해야하는지 잘 모르겠다. 당신은 이미 tablerestaurantarray에 그들을 가지고 있으므로, 그들을 캐싱 어떤 이점이되지 않습니다. 어쩌면 이미지를 캐싱하려는 의도일까요?

+0

이 이미지는 클라우드 키트에서 다운로드되었으며 캐시가 생성 될 때까지 tableview 애니메이션이 끊어졌습니다. 적어도 내 생각은 그렇습니다. – Jon

+0

내가 추가해야하는 것은 ckrecords의 이미지가 이미지 데이터를 받기 위해 클래스를 통과 한 것입니다. – Jon

+0

제안 된 "let key = 'restaurant \ (indexpath)"가 불행히도 작동하지 않았습니다. – Jon

관련 문제