我有一個TableView的界面,是一個列表頁,只有一個Section,每一個Cell里都有一張圖片是從服務器異步加載的。我用ASIHttpRequest去加載圖片,在每個Request里已經通過UserInfo帶上了這個Cell的NSIndexPath.row,然后回調的時候,就按照這個NSIndexPath去更新Cell的默認占位圖片。但是,我發現刷兩下以后,Cell里的圖片又亂掉了。貌似我NSIndexPath.row沒用一樣的,有沒有人也遇到過一樣的問題啊。完整代碼太復雜,把涉及到網絡的代碼貼一下-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{// ...
[self loadImageWithURL:[self.dataSource objectAtIndex:indexPath.row]
index:indexPath.row]; // 這是我自己定義的一個方法// ...}
- (void)loadImageWithURL:(NSString *)aURL index:(NSInteger)anIndex {
ASIHTTPRequest *asiRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:aURL]];
asiRequest.delegate = self;
asiRequest.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%d", anIndex], @"index", nil];
asiRequest.requestMethod = @"GET";
[asiRequest startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest*)request { NSInteger index = [[request.userInfo objectForKey:@"index"] intValue]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:index
inSection:0]];
cell.imageView.image = [UIImage imageWithData:request.responseData];}
2 回答

精慕HU
TA貢獻1845條經驗 獲得超8個贊
UITableView 對 cell 會有重用機制,以保證較少的內存使用。而帶來的問題是,如果代碼寫得不仔細,可能會導致某行 cell 的內容出現在另一行。
把 cellForRowAtIndexPath 的完整代碼貼出來吧。

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
這個問題我曾經也遇到過,我的解決方案是:在每一個ImageView的對象中記錄該圖片的URL,當異步請求回調,要展示圖片的時候,先檢查URL是否一致,如果不一致,就先把請求下來的圖片丟進緩存。
- 2 回答
- 0 關注
- 174 瀏覽
添加回答
舉報
0/150
提交
取消