2015-09-05 5 views
5

स्क्रॉल करते समय सेल छवियां बदल रही हैं हाय मेरी समस्या यह है कि जब मैं तालिका दृश्य को स्क्रॉल करता हूं तो छवि गलत सेल में दिखाई देगी, कुछ सेकंड के बाद सही छवि दिखाई देती है।UITableView -

यहाँ मेरी कोड

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

// Configure the cell... 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 
} 

[cell setOpaque:NO]; 
[cell setBackgroundColor: [UIColor clearColor]]; 

PlaceData *data = [tableData objectAtIndex:indexPath.row]; 
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100]; 
UILabel *sciNameLabel = (UILabel *)[cell viewWithTag:200]; 
UIImageView *thumbnailImageView = (UIImageView *)[cell viewWithTag:300]; 
nameLabel.text = data.name; 
sciNameLabel.text = data.scientific_name; 


// get a dispatch queue 
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
// this will start the image loading in bg 
dispatch_async(concurrentQueue, ^{ 
    NSURL *urlToPicture = [NSURL URLWithString:[NSString stringWithFormat:@"%@", data.thumbnail]]; 
    NSData *imgData = [NSData dataWithContentsOfURL:urlToPicture options:0 error:nil]; 

    // This will set the image when loading is finished 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     UIImage *tmpImage = [[UIImage alloc] initWithData:imgData]; 
     thumbnailImageView.image = tmpImage; 
     //dispatch_release(concurrentQueue); 
     }); 
    }); 

     return cell; 
} 

कृपया मेरी मदद

+0

https://stackoverflow.com/questions/43866409/collectionview-in-tableviewcell-scroll-isnt-smooth-in-swift3/43907018 # 43907018 – Sanju

उत्तर

18

आप अपने cellForRowAtIndexPath के लिए निम्न कोड जोड़ने की कोशिश कर सकते हैं -

1) अपने कस्टम सेल करने के लिए एक सूचकांक मान निर्दिष्ट करें। उदाहरण के लिए,

cell.tag = indexPath.row 

2) मुख्य थ्रेड पर, चित्र को सौंपने से पहले, देखें कि क्या छवि टैग के साथ मिलान करके इसी सेल अंतर्गत आता है।

dispatch_async(dispatch_get_main_queue(), ^{ 
    if(cell.tag == indexPath.row) { 
     UIImage *tmpImage = [[UIImage alloc] initWithData:imgData]; 
     thumbnailImageView.image = tmpImage; 
    }}); 
}); 
+2

उत्तर के लिए धन्यवाद !! इससे मुझे समस्या हल करने में मदद मिलती है। –

+1

बेस्ट उत्तर, मेरी समस्या हल हो गई – Jan

4

आप पुराने कोशिकाओं है, जो एक अच्छी बात है पुन: उपयोग कर रहे हैं। हालांकि, आप सेल के छवि दृश्य में छवि को प्रारंभ नहीं कर रहे हैं, जो ऐसी अच्छी बात नहीं है। आप जो वर्णन कर रहे हैं वह होता है क्योंकि एक पुरानी सेल, उस छवि के साथ जो पहले से ही उस सेल के लिए लोड की गई थी, का उपयोग नए सेल के लिए किया जाता है। फिर आप उस सेल की छवि को पृष्ठभूमि में लोड कर रहे हैं (जो, फिर से, अच्छा है) लेकिन उस छवि के लिए पूरी तरह लोड होने में कुछ पलों लगते हैं। इस बीच, पुरानी सेल पर पहले से लोड की गई छवि को प्रदर्शित किया गया है (और यही वजह है कि आप उस सेल में कुछ क्षणों के लिए गलत छवि देख रहे हैं)।

समाधान? जोड़ने या तो

thumbnailImageView.image = nil 

या

thumbnailImageView.image = someDefaultImageWhileYourRealOneLoads 

सही dispatch_queue_t concurrentQueue ... से पहले।

इस तरह, वास्तविक लोड होने पर आपको पुरानी (अप्रासंगिक) छवि दिखाई नहीं देगी।

मुझे उम्मीद है कि इससे मदद मिलती है। सौभाग्य।

+0

उत्तर के लिए धन्यवाद लेकिन यह अभी भी पुरानी छवि –

+1

प्रदर्शित करता है, धन्यवाद, कुछ घंटों के लिए समाधान देख रहा है और आपका सबसे आसान और सर्वोत्तम है एक! – DazChong

3

क्योंकि आपकी छवि दृश्य को async प्रेषण कॉल में लोड किया जा रहा है जो मुख्य धागे पर नहीं है और इसे किसी अन्य धागे में बुलाया जा रहा है, इसलिए यूआरएल से डेटा लाने में देरी हो रही है और फिर इसे यूआईएममेज में परिवर्तित करने में देरी हो रही है । इस प्रक्रिया में कुछ समय लगता है जैसा कि आप जानते हैं लेकिन आप टेबलव्यू को तेज दर से स्क्रॉल कर रहे हैं। और जैसा कि आप जानते हैं cellForRowAtIndexPath खिड़की से बाहर होने वाले किसी भी सेल का पुन: उपयोग करता है, इसलिए जिस सेल का पुन: उपयोग किया जा रहा है, वह इमेजेटाटा नहीं ला सकता है कि यह विंडो में होने पर पहले इसे पुनः प्राप्त करना था। इस प्रकार यह गलत डेटा लोड करता है और फिर फिर async उस विशिष्ट सेल के लिए निकाल दिया जाता है, सेल उस छवि को लोड करता है लेकिन देरी होती है।

इस सुविधा को दूर करने के रूप में Chronch यह बताया यू शून्य के रूप में imageView छोड़ सकते हैं या आप AFNetworking की खुद UIImageView वर्ग जो imageView छवियों को लोड करने के लिए काफी सुंदर ढंग से

मैं ua छोड़ देंगे एक शानदार वर्ग है उपयोग कर सकते हैं लिंक करने के लिए इसे AFNetworking

+0

उत्तर के लिए धन्यवाद –

1

मैं अपने सभी डेटा - tableView:willDisplayCell:forRowAtIndexPath: पर बाध्यकारी सिर्फ इसलिए cellForRowAtIndexPath में अपने सेल अभी तक तैयार नहीं किया गया है करना होगा। एक अन्य समाधान जिसका आप उपयोग कर सकते हैं वह AFNetworking है जो किसी और ने मेरे सामने उल्लेख किया है।

+1

उत्तर के लिए धन्यवाद –

1

स्विफ्ट 3।0

DispatchQueue.main.async(execute: {() -> Void in 

    if cell.tag == indexPath.row { 
     var tmpImage = UIImage(data: imgData) 
     thumbnailImageView.image = tmpImage 
    } 
}) 
0

दो पंक्तियाँ जोड़ें चिंता और फ्री महसूस

cell.thumbnailimages.image=nil 

    cell.thumbnailimages.setImageWith(imageurl!) 

मुझे लगता है कि इन दो पंक्तियों अग्रिम में आपकी समस्या का समाधान धन्यवाद मत करो