मैं विभिन्न आयामों में छवियों वाले कोशिकाओं के लिए अपने पुन: प्रयोज्यसेल का उपयोग करने का प्रयास कर रहा हूं। छवियों को 220x150 ब्लैक बॉक्स के अंदर रखा गया है जिसमें स्केलिंग UIViewContentModeScaleAspectFit
है।विभिन्न आयामों में छवियों के साथ UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NewsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NewsItem *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.imageUrl]];
[cell.imageView setImage:[[UIImage alloc] initWithData:data]];
[cell.imageView setBackgroundColor:[UIColor blackColor]];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
CGRect imageViewFrame = cell.imageView.frame;
imageViewFrame.size.width = 220;
imageViewFrame.size.height = 150
[cell.imageView setFrame:imageViewFrame];
[cell.textLabel setText:item.title];
return cell;
}
उपर्युक्त कोड नीचे दिए गए लेआउट में परिणाम देता है और तालिका दृश्य में स्क्रॉल करते समय छवियां कभी-कभी बदलती रहती हैं। छवियों
इस असंरचित लेआउट के बजाय, मैं चाहते हैं इस तरह गठबंधन किया जाना:
क्या मैं इस ReusableCell के साथ कुछ गलत कर रहा हूँ?
EDIT1:
मैं एक imageView बना सकते हैं और cell.contentView करने के लिए एक superview के रूप में इस imageView जोड़ने की कोशिश कर रहा हूँ।
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NewsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NewsItem *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImage *placeholderImage = [UIImage imageNamed:@"ImagePlaceholderThumb"]; //220x150
UIImageView *imageView = [[UIImageView alloc] initWithImage:placeholderImage];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.imageUrl]];
[imageView setImage:[[UIImage alloc] initWithData:data]];
[imageView setBackgroundColor:[UIColor blackColor]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
CGRect imageViewFrame = imageView.frame;
imageViewFrame.size.width = placeholderImage.size.width;
imageViewFrame.size.height = placeholderImage.size.height;
[imageView setFrame:imageViewFrame];
[cell.contentView addSubview:imageView];
[cell.textLabel setText:item.title];
return cell;
}
निम्नलिखित में से ऊपर कोड परिणाम:
यह छवियों में से कुछ की तरह है दो कोशिकाओं में दिखाई दे रहे हैं। ऐसा लगता है कि वे imageViewFrame में सेट किए गए आकार को नहीं रख रहे हैं। तुम जानते हो क्यों?
जो आप खोज रहे हैं उसे पत्र मुक्केबाजी और AFAIK कहा जाता है जो कि यूआईकिट आपके लिए तब तक नहीं करेगा जब तक आप इसे स्वयं नहीं करते। – Till
तो, मैं यह कैसे कर सकता हूं? – dhrm
मेरे अपने उत्तर पर टिप्पणी देखें। साथ ही, एक तरफ, जिस तरह से आप UIImageView बना रहे हैं और उसका डेटा लोड कर रहे हैं, हर बार सेल ऑफ स्क्रीन से दिखाई देता है। आप कुछ स्केची स्क्रॉलिंग देख सकते हैं। मैं दो चीजों को अलग-अलग करता हूं: 0) छवि डेटा को असीमित रूप से लोड करें और इसे EGOImageView जैसे कुछ के साथ कैश करें, 1) आम तौर पर, जब आप सेल बनाते हैं तो आप सेल के सबव्यूव्स बनाना चाहते हैं, तो आप केवल उप-दृश्यों की सामग्री को बदलना चाहते हैं// सेल को कॉन्फ़िगर करें ... – Keller