How to show images in UITableViewCell From NSMutableArray? - uitableview

I'm doing this :
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
cell.imageView.image = [UIImage imageNamed:[[self.articlesArray
objectAtIndex:indexPath.row]objectForKey:#"images"]];
// [cell.imageView setImageWithURL:[NSURL URLWithString:[[self.articlesArray
objectAtIndex:indexPath.row]objectForKey:#"images"]]];
}
return cell;
}
I'm doing this:
cell.imageView.image = [UIImage imageNamed:[[self.articlesArray
objectAtIndex:indexPath.row]objectForKey:#"images"]];
But nothing is showing in my UITableViewCell.
And I want to show image in left of tableViewCell on left side ,
Please tell me how can I do this.

I've done it by myself.
here is the solution, and it works perfectly.
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:
[[self.articlesArray objectAtIndex:indexPath.row]objectForKey:#"images"]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.imageView.image =image;

Related

why isnt it let me crop my image into a circle with this code?

This is throwing an error. It is my first time trying to do this part, so bear with me.
My error is at cell.image.frame...:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
PFObject *group = groups[indexPath.row];
//if(group[PF_GROUP_LOGO] != nil){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ // go to a background thread to load the image and not interfere with the UI
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:group[PF_GROUP_LOGO]]]];
dispatch_async(dispatch_get_main_queue(), ^{ // synchronize back to the main thread to update the UI with your loaded image
cell.layer.cornerRadius = cell.image.frame.size.width/2;
cell.layer.masksToBounds = YES;
cell.imageView.image = image;
cell.textLabel.text = group[PF_GROUP_NAME];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d users", (int) [group[PF_GROUP_MEMBERS] count]];
cell.detailTextLabel.textColor = [UIColor lightGrayColor];
});
});
return cell;
}
I figured it out! I was just referencing cell. in an inappropriate way.
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
PFObject *group = groups[indexPath.row];
//if(group[PF_GROUP_LOGO] != nil){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ // go to a background thread to load the image and not interfere with the UI
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:group[PF_GROUP_LOGO]]]];
dispatch_async(dispatch_get_main_queue(), ^{ // synchronize back to the main thread to update the UI with your loaded image
cell.imageView.image = image;
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height /2;
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.borderWidth = 0;
cell.textLabel.text = group[PF_GROUP_NAME];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d users", (int) [group[PF_GROUP_MEMBERS] count]];
cell.detailTextLabel.textColor = [UIColor lightGrayColor];
});
});
return cell;
}

UITableview with images scroll very slowly

I have an UITableView which downloads its UITableViewCells images from a server.
I observed that the table scrolls very slowly.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"parallaxCell";
JBParallaxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSURL *imageURL = [NSURL URLWithString:[[news objectAtIndex:indexPath.row]objectForKey:#"Resim"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.titleLabel.text = [[news objectAtIndex:indexPath.row]objectForKey:#"Adi"];
cell.subtitleLabel.text = [[news objectAtIndex:indexPath.row]objectForKey:#"Resim"];
cell.parallaxImage.image = imageLoad;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
You are loading image file on main thread and this operation is slowing your scroll. Use UIImageView+AFNetworking.h from AFNetworking to speed up your app by async image loading. link https://github.com/AFNetworking/AFNetworking
I use this Library which is just perfect
SDWebImage
You just need to #import <SDWebImage/UIImageView+WebCache.h> to your project, and you can define also the placeholder when image is being downloaded with just this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.textLabel.text = #"My Text";
return cell;
}
It also cache downloaded images and gives you great performance.
Hope it will help you!
Load the images asynchronously. It will help you :
Loading an image into UIImage asynchronously
Link

UIImage is showing on simulator, not on a real device

I have tried all of the suggestions in other similarly titled questions. My case seems to be special. Here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if ([menu_List count] == 0){
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSString *newString = [[MainMenuArray objectAtIndex:indexPath.row] stringByReplacingOccurrencesOfString:#" " withString:#""];
UIImage *image1 = [UIImage imageNamed:[NSString stringWithFormat:#"%#%#", newString,#".png"]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image1];
[imageView setFrame:CGRectMake(5.0, 1.0, 41.0, 41.0)];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[cell addSubview:imageView];
cell.textLabel.textColor = [UIColor whiteColor];
GetData *GD = [[GetData alloc] init];
cell.backgroundColor = [GD colorWithHexString:#"18204a"];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [MainMenuArray objectAtIndex:indexPath.row]];
}else{
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", [menu_List objectAtIndex:indexPath.row]];
cell.textLabel.textColor = [UIColor whiteColor];
GetData *GD = [[GetData alloc] init];
cell.backgroundColor = [GD colorWithHexString:#"18204a"];
}
return cell;
}
So the string name is correct, and it matches the case. I've checked my build settings, and the images are in copy bundle resources. All show up in the simulator. Only two of the seven appear in the list. If I change the order in which they appear, it is still the same two images that show up. They all show up in the simulator. I am using ECSlidingViewController to create a menu and I want the images to show up. Any idea on what could be causing it?
Clean your project and check for upper case letters. Simulator is not case sensitive, but real devices does not load an image if you use "myimage.png" on your code but your image name is MyImage.png
Make sure your png is enabled for the schema

property not found on object UiTableViewCell

i'm trying to show fetch data from core data into my tableview the problem is i get the.
´Property yttitle not found on object of type UITableView´
i've made sure the cell identifier is correct and the yttitle and the other are synthesized correctly. Why am i getting this error?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
// Configure the cell...
NSManagedObject *device = [devices objectAtIndex:indexPath.row];
cell.ytTitle.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"songName"]];
cell.ytAuthor.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"author"]];
[cell.ytThumbnail.layer setBorderColor: [[UIColor grayColor] CGColor]];
[cell.ytThumbnail.layer setBorderWidth: 1.0];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"%#", [device valueForKey: #"link"]]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.ytThumbnail.image = image;
return cell;
}
UITableViewCell doesn't have that properties, you have to create your own custom cell view and load it, you can use this code to load it from an xib
NSArray *xibObj = [[NSBundle mainBundle] loadNibNamed:#"CustomTVCell" owner:nil options:nil];
for(id currentObj in xibObj){
if ([currentObj isKindOfClass:[CustomTVCell class]]) {
cell = (CustomTVCell *) currentObj;
}
}
Probably If I understand what you want to do, creating a custom cell is not necessary. You can simply use the UITableViewCell with UITableViewCellStyleSubtitle and use the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
// Configure the cell...
NSManagedObject *device = [devices objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"songName"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"author"]];
[cell.imageView.layer setBorderColor: [[UIColor grayColor] CGColor]];
[cell.imageView.layer setBorderWidth: 1.0];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"%#", [device valueForKey: #"link"]]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = image;
return cell;
}
It's complaining because you try to call this property (yttitle) on UITableViewCell object and there isn't any property like that. You should cast UITableViewCell to your custom class like that:
YOURCUSTOMTableViewCell *cell = (YOURCUSTOMTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[YOURCUSTOMTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
And make sure your YOURCUSTOMTableViewCell contain the property (yttitle, etc).
Hope this help.
Are you using custom cell for this TableView? Note that your method is return and using standard UITableViewCell, and this standard cell don't have a #propoerties like ytTitle and ytAuthor.
So if you're using custom cell you have to change it in three places in your code:
- (MyCustomCellClass *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCellClass *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[MyCustomCellClass alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
...
If you declare properties in your view controller (this don't make sense) you should move it to custom cell and get rid of it in VC.

Not able to view image in tableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
cell.detailTextLabel.text = [[temp objectForKey:#"distance"] stringByAppendingString:#" km from Banglore"];
cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
return cell;
}
Change your cell's style to UITableViewCellStyleDefault
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
If you want two labels in the cell, then add it to the contentView of the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
UIlabel *detailLbl = [[UILabel alloc]initWithFrame:CGRectMake(60,10,200,50)];
[detailLbl setText:[[temp objectForKey:#"distance"] stringByAppendingString:#" km from Banglore"];];
[cell.contentView addSubView:detailLbl];
cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
return cell;
}
Try this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
cell.detailTextLabel.text = [[temp objectForKey:#"distance"] stringByAppendingString:#" km from Banglore"];
//cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
//write this code
// Load the image with an GCD block executed in another thread
dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(downloadQueue, ^{
UIImage * image = [UIImage imageNamed:[temp objectForKey:#"category"]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = image;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
[cell setNeedsLayout];
});
});
dispatch_release(downloadQueue);
return cell;
}

Resources