Currently I'm making a Messaging App but recently the tableCells are not resizing as expected. I've used the new iOS 8 relative height functionality but still nothing changes. All the design it's being done via Storyboard so far everything works as expected but need to tell the tableCell to resize based on the textview height. here how it's currently looking.
http://i.imgur.com/lqoxxJV.png
The Code I'm using is the following.
- (UITableViewCell*)tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary* chatMessage = [_conversation objectAtIndex:indexPath.row];
// Show If type is String
if ([chatMessage[#"type"] isEqualToString:#"string"]) {
// if its me
if([chatMessage[#"user_sender"] isEqualToString:_user_sender]){
NSString *CellIdentifier = #"fromMe";
FromMeTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.message_me setNeedsLayout];
[cell.message_me layoutIfNeeded];
cell.message_me.clipsToBounds = YES;
cell.message_me.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 7.0, 8.0);
cell.message_me.text = chatMessage[#"msg"];
cell.message_date_me.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
cell.avatar_message_me.clipsToBounds = YES;
cell.avatar_message_me.layer.cornerRadius = cell.avatar_message_me.frame.size.width /2;
cell.avatar_message_me.image = [UIImage imageNamed:#"amber.png"];
return cell;
}
// it its other user
else {
NSString *CellIdentifier = #"fromThem";
FromThemTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.message_them setNeedsLayout];
[cell.message_them layoutIfNeeded];
cell.message_them.clipsToBounds = YES;
cell.message_them.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 7.0, 8.0);
cell.message_them.text = chatMessage[#"msg"];
cell.message_date_them.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
cell.avatar_message_them.clipsToBounds = YES;
cell.avatar_message_them.layer.cornerRadius = cell.avatar_message_them.frame.size.width /2;
cell.avatar_message_them.image = [UIImage imageNamed:#"jenny.png"];
return cell;
}
}
// Show if type is Image File
else if ([chatMessage[#"type"] isEqualToString:#"img"]){
// if its me
if(![chatMessage[#"user_sender"] isEqualToString:_user_sender]){
NSString *CellIdentifier = #"fromThemImage";
FromThemImageTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
cell.image_type_them.image = [UIImage imageNamed:#"foto.jpeg"];
cell.message_date_them.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
return cell;
}
// if its other user
else {
NSString *CellIdentifier = #"fromMeImage";
FromMeImageTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
cell.image_type_me.image = [UIImage imageNamed:#"foto.jpeg"];
cell.message_date_me.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
return cell;
}
}
else if ([chatMessage[#"type"] isEqualToString:#"file"]){
NSLog(#"Type: %#", chatMessage[#"type"]);
NSString *CellIdentifier = #"fromThemFile";
FromThemFileTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
cell.message_date_them.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
cell.file_name_them.text = chatMessage[#"msg"];
cell.file_type_them.contentMode = UIViewContentModeScaleAspectFill;
NSArray *formats = [chatMessage[#"msg"] componentsSeparatedByString:#"."];
// PDF Format
if ([formats[1] isEqualToString:#"pdf"]) {
cell.file_type_them.image = [UIImage imageNamed:#"pdf.png"];
}
// Word Format
if ([formats[1] isEqualToString:#"doc"]) {
cell.file_type_them.image = [UIImage imageNamed:#"doc.png"];
}
if ([formats[1] isEqualToString:#"docx"]) {
cell.file_type_them.image = [UIImage imageNamed:#"doc.png"];
}
// Power Point Format
if ([formats[1] isEqualToString:#"pptx"]) {
cell.file_type_them.image = [UIImage imageNamed:#"ppt.png"];
}
if ([formats[1] isEqualToString:#"ppt"]) {
cell.file_type_them.image = [UIImage imageNamed:#"ppt.png"];
}
// Excel Format
if ([formats[1] isEqualToString:#"xls"]) {
cell.file_type_them.image = [UIImage imageNamed:#"xls.png"];
}
if ([formats[1] isEqualToString:#"xlsx"]) {
cell.file_type_them.image = [UIImage imageNamed:#"xls.png"];
}
return cell;
}
else {
// Remember to set this as a default value
NSString *CellIdentifier = #"fromThem";
FromThemTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
cell.message_them.text = chatMessage[#"msg"];
cell.message_date_them.text = [NSString stringWithFormat:#"%#", chatMessage[#"message_date"]];
return cell;
}
}
The constraints I've been using for the textview to be able to expand and look like this is the following.
http://i.imgur.com/Kjva3ES.png
Any Idea how to fix this on a decent way..?
If your UITableViewCells vary in height due to variations in content you need to implement
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath;
to tell UITableView the height for each row.
In one of my apps this mainly involved calling
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context;
to predict the area needed by my expanding text field, and then adding in some margin.
Related
I have a few custom cells that I would like to display information on. I made 2 new cells however it is only returning one cell. The top most cell's identifier is "Cell" and the bottom one is "quantityCell". I would like the product description to be on the top cell and the quantity on the bottom cell. The output I am getting now is only the quantity cell showing, but not the product cell. If I remove the quantity cell, the product cell will show. How should I go about doing this?
Thank you
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *quantityCellIdentifier = #"quantityCell";
UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];
DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];
self.productName = [inventoryItem getProductTitle];
productTitle = self.productName;
cell.detailTextLabel.text = productTitle;
NSLog(#"productTitle %#", self.productName);
cell.textLabel.text = #"Product Title";
self.productQuantity = [inventoryItem getQuantity];
quantityCell.textLabel.text = #"Quantity";
quantityCell.detailTextLabel.text = self.productQuantity;
/*
// Configure the cell...
// cell.textLabel.text = productTitle;
cell.textLabel.text = #"Product Name";
cell.detailTextLabel.text = productTitle;
*/
return cell;return quantityCell;
}
Problem
The you can't return 2 parameters in the same time on a method. So where you say:
return cell;return quantityCell;
Only cell is being returned, the method ends on the first return.
Solution
You need to make logic which one time returns topCell and the other time bottomCell
So in your case you just need to have indexPath.row modulus 2, which can take any value and returns only 0 on even numbers and 1 on odd numbers.
So your code should be like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"";
if (indexPath.row%2==0) {
//Get the top cell
CellIdentifier = #"Cell";
} else {
//Get bottom cell
CellIdentifier = #"quantityCell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];
if (indexPath.row%2==0) {
//Customize topCell
self.productName = [inventoryItem getProductTitle];
productTitle = self.productName;
cell.detailTextLabel.text = productTitle;
NSLog(#"productTitle %#", self.productName);
cell.textLabel.text = #"Product Title";
} else {
//Customize bottom cell
self.productQuantity = [inventoryItem getQuantity];
cell.textLabel.text = #"Quantity";
cell.detailTextLabel.text = self.productQuantity;
}
return cell;
}
return cell;
return quantityCell;
The above 2 line you wrote in your function! After return statement the method stop executing further lines.
You could do something like this:
self.productName = [inventoryItem getProductTitle];
self.productQuantity = [inventoryItem getQuantity];
productTitle = self.productName;
if(indexPath.row == 0) {
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.detailTextLabel.text = productTitle;
cell.textLabel.text = #"Product Title";
return cell;
}
else {
NSString *quantityCellIdentifier = #"quantityCell";
UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];
quantityCell.textLabel.text = #"Quantity";
quantityCell.detailTextLabel.text = self.productQuantity;
return quantityCell;
}
Hope this helps.. :)
Thank you all, I ended up using a dictionary that returns all of the keys and values to the cells.
NSDictionary *inventoryDictionary = #{
#"Quantity" : productQuantity,
#"Price" : productPriceValue,
#"Product Title" : productTitle
};
NSLog(#"ProductStrings: %#", inventoryDictionary);
NSString *keys = [inventoryDictionary allKeys][indexPath.row];
NSString *providerNameString = keys;
cell.textLabel.text = providerNameString;
NSString *Values = [inventoryDictionary allValues][indexPath.row];
NSString *providerNameValue = Values;
cell.detailTextLabel.text = providerNameValue;
return cell;
I have two UIViewControllers with tableview. When the first cell loads in the second UIViewController it calls the cellForRowAtIndexPath in the same class but when it loads the second cell it calls the first viewControllers cellForRowAtIndexPath.
My code as follows:
SecondViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NotificationsTableViewCell *cell = [self.notificationsTableView dequeueReusableCellWithIdentifier:#"NotificationCell"];
if(cell == nil)
{
cell = [[NotificationsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"NotificationCell"];
}
NSMutableDictionary *cellData = [self.databaseCall transactionFromDatabase:indexPath.row];
NSLog(#"%#", cellData);
cell.goalNameLabel.text = [cellData objectForKey:#"categoryName"];
NSString *cardTypeId = [cellData objectForKey:#"cardTypeId"];
NSString *tipsId = [cellData objectForKey:#"tipsId"];
if([self.defaultCardTypeId containsObject:cardTypeId])
{
NSUInteger index = [self.defaultCardTypeId indexOfObject:cardTypeId];
[self.defaultCardTypeId replaceObjectAtIndex:index withObject:cardTypeId];
}
else{
[self.defaultCardTypeId addObject:cardTypeId];
}
if([self.defaultTipId containsObject:tipsId])
{
NSUInteger index = [self.defaultCardTypeId indexOfObject:cardTypeId];
[self.defaultTipId replaceObjectAtIndex:index withObject:cardTypeId];
}
else{
[self.defaultTipId addObject:tipsId];
}
if([cardTypeId isEqualToString:#"1"])
{
UIImage *cellImage = [UIImage imageNamed:#"icon2.jpg"];
cell.cardTypeImage.image = cellImage;
cell.cardTypeLabel.text = #"GOOD TO KNOW";
cell.cardTypeLabel.textColor = [UIColor colorWithRed:252/255.0 green:171/255.0 blue:19/255.0 alpha:1];
}
if([cardTypeId isEqualToString:#"2"])
{
UIImage *cellImage = [UIImage imageNamed:#"icon1.jpg"];
cell.cardTypeImage.image = cellImage;
cell.cardTypeLabel.text = #"TO CONSIDER";
cell.cardTypeLabel.textColor = [UIColor colorWithRed:0/255.0 green:191/255.0 blue:243/255.0 alpha:1];
}
cell.notificationCard.layer.cornerRadius = 5;
// Configure the cell...
return cell;
}
FirstViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GoalsCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"GoalsListCell" forIndexPath:indexPath];
if(cell == nil)
{
cell = [[GoalsCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GoalsListCell"];
}
NSInteger indexOfCategory = [self.databaseCall.arrColumnName indexOfObject:#"CategoryName"];
NSInteger indexOfImage = [self.databaseCall.arrColumnName indexOfObject:#"CategoryImage"];
NSInteger indexOfActive = [self.databaseCall.arrColumnName indexOfObject:#"coulmn"];
//Assigning the contents of cell
cell.goalName.text = [NSString stringWithFormat:#"%#", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfCategory]];
NSString *categoryImage = [NSString stringWithFormat:#"%#", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfImage]];
NSString *activeStatus = [NSString stringWithFormat:#"%#", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfActive]];
UIImage *cellImage = [UIImage imageNamed:categoryImage];
cell.goalImage.image = cellImage;
[cell.favouriteButton addTarget:self action:#selector(favouriteButtonPressed:) forControlEvents:UIControlEventTouchDown];
NSMutableString *selectedRowImage = [[NSMutableString alloc] initWithString:#""];
//Checking whether the category is selected by user or not
if([activeStatus isEqualToString:#"yes"])
{
selectedRowImage = [NSMutableString stringWithFormat:#"starsel.png"];
}
else
{
selectedRowImage = [NSMutableString stringWithFormat:#"stardef.png"];
}
UIImage *favouriteIconImage = [UIImage imageNamed:selectedRowImage];
[cell.favouriteButton setBackgroundImage:favouriteIconImage forState:UIControlStateNormal];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
return cell;
}
Thanks in advance.
First of all i would say sorry for this stupid question.
The problem is due to the tableview datasource as specifies by #Paulw11, #Onik IV, #Kannan Vora. The secondViewController tableView has the datasource of firstViewController.
What I want to do here is set it so that if top3ArrayForSection.count-1 < 1, set the _results bool value of that respective section to NO, and so on. What's happening instead, is that _results is set to NO or YES for the entire table overall, so that I end up with a result like this:
When only the "xperia Z3 compact unlocked" section should say "no items found etc." because it has no cells, the other sections cells shouldn't.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSectionDictionary = _matchCenterArray[section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
if (top3ArrayForSection.count-1 < 1){
_results = NO;
_rowCount = 1;
}
else if(top3ArrayForSection.count-1 >= 1){
_results = YES;
_rowCount = top3ArrayForSection.count-1;
}
return _rowCount;
}
// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// No cell seperators = clean design
tableView.separatorColor = [UIColor clearColor];
if (_results == NO) {
// title of the item
cell.textLabel.text = #"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
else if (_results == YES) {
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
}
return cell;
}
Your instance variable _results is one-dimensional. You could replace it with an NSArray and store the values individually, or you could change the logic in your code as such:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSectionDictionary = _matchCenterArray[section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
return (top3ArrayForSection.count-1 < 1) ? 1 : top3ArrayForSection.count-1;
}
// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// No cell seperators = clean design
tableView.separatorColor = [UIColor clearColor];
NSDictionary *currentSectionDictionary = _matchCenterArray[indexPath.section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
if (top3ArrayForSection.count-1 < 1) {
// title of the item
cell.textLabel.text = #"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
else {
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
}
return cell;
}
I'm struggling with creating a tableview with custom, subclassed cells created in storyboards.
All the cells work well except the "questionCell". This cell only shows a fragment of its textfield (or sometimes none of it) until I scroll down and then back up again.
The initial appearance:
After scrolling down then up again:
Where am I going wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"TextCell";
static NSString *CellIdentifier2 = #"QuestionCell";
static NSString *CellIdentifier3 = #"ImageCell";
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
UITableViewCell *cell = nil;
if (section == 0){
//the intro section
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
textCell.labelText.text = _intro;
cell = textCell;
} else if (section == 4){
//this is the "the answer is . . ." section
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
textCell.labelText.text = [NSString stringWithFormat:#"The only EOM to not be eliminated is %#",_theAnswer];
cell = textCell;
} else {
if (row == 0) {
NSString *item = [NSString stringWithFormat:#"question_%li", (long) section];
NSString *txt = [self valueForKey: item];
QuestionCell *qCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
qCell.questionText.text = txt;
cell = qCell;
} else if (row == 1) {
NSString *item = [NSString stringWithFormat:#"img_%li", (long)section];
NSString *img = [self valueForKey: item];
ImageCell *iCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3 forIndexPath:indexPath];
iCell.image.image = [UIImage imageNamed: img];
cell = iCell;
} else {
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
NSString *item = [NSString stringWithFormat:#"explain_%li", (long)section];
NSString *txt = [self valueForKey: item];
textCell.labelText.text = txt;
cell = textCell;
}
}
return cell;
}
Here's heightForRowAtIndexPath. The results are the same if I just comment this out and set the heights in the prototype cells.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// Return NO if you do not want the specified item to be editable.
CGFloat hgt = 70;
if (indexPath.section == 0){
//the intro
hgt = 150;
} else if (indexPath.section == 4){
//the "answer is . . ."
hgt = 100;
} else {
if (indexPath.row == 1) {
//the image
hgt = 225;
} else if (indexPath.row == 2){
//the discussion
hgt = 180;
}
}
return hgt;
}
the questionCell just sets the iboutlet:
#import <UIKit/UIKit.h>
#interface QuestionCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UITextView *questionText;
#end
I'm developing and iOS app and my UITableView is kinda slow. I would like to know all the possible reasons for a slow scroll.
Note that I'm using prototype cells in the storyboard and I don't have any images.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
Classification *classification = [classifications objectAtIndex:indexPath.row];
UILabel *teamLabel = (UILabel *) [cell viewWithTag:101];
[teamLabel setText:[classification team]];
[cell setBackgroundColor:[UIColor clearColor]];
return cell;
}
Also, it runs smoothly in the simulator.
I'm using an iPad 2.
Download the data asynchronously in your:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Use 2 separated UITableViewCell objects, one to fill with text data, one for images which need to be in thread such as dispatch_async, example from my project:
if (imageUrl.length > 0)
{
NSString *completeURL = #"";
completeURL = [completeURL stringByAppendingString:kPROFILE_IMAGE_URL];
completeURL = [completeURL stringByAppendingString:#"/2/"];
completeURL = [completeURL stringByAppendingString:imageUrl];
completeURL = [completeURL stringByAppendingString:#".png"];
NSString *fileName = [NSString stringWithFormat:#"%#.png", imageUrl];
NSString* path = GetMediaFolder();
BOOL imageExists = [[NSFileManager defaultManager] fileExistsAtPath:[path stringByAppendingPathComponent:fileName]];
if (imageExists)
{
// NSLog(#"Image exists");
// Update UI
dispatch_async(dispatch_get_main_queue(), ^
{
UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = (UIImageView*)[updateCell viewWithTag:kTAG_IMAGE];
imageView.image=[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:fileName]];
imageView.layer.cornerRadius = 23.0;
imageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]);
imageView.layer.borderWidth = 1.0;
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[updateCell setNeedsLayout];
});
}
else
{
// NSLog(#"Image not exists");
// Download The Image
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
NSURL *imageURL=[NSURL URLWithString:completeURL];
NSData *image=[NSData dataWithContentsOfURL:imageURL];
[image writeToFile:[path stringByAppendingPathComponent:fileName] atomically:YES];
// Update UI
dispatch_async(dispatch_get_main_queue(), ^
{
UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = (UIImageView*)[updateCell viewWithTag:kTAG_IMAGE];
imageView.image=[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:fileName]];
imageView.layer.cornerRadius = 23.0;
imageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]);
imageView.layer.borderWidth = 1.0;
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[updateCell setNeedsLayout];
});
});
}
}
}
return cell;
EDIT 1:
I see you don't use images, you still need to download all the data asynchronously and fill it in the new UITableViewCell when download finished (instead of my images downloads, temporary data and downloaded data).