Images appear after scrolling table view (AFNetworking) - iphone

I am using AFNetworking to download images from server , everything works fine but images wont't be appear unless I scroll table view . here is my code :
- (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];
}
//configuring news feed
dictionary = [newsFeed objectAtIndex:indexPath.row];
//display thumbnails
UIImageView *image = [[UIImageView alloc]init];
[image setImageWithURL:[NSURL URLWithString:[dictionary objectForKey:#"image"]]placeholderImage:[UIImage imageNamed:NEWSAVATAR]];
cell.imageView.image = [image.image imageScaledToSize:CGSizeMake(55, 55)];
[cell setNeedsLayout];
return cell;
}

Try this
[ cell.imageView.image setImageWithURL:[NSURL URLWithString:[dictionary objectForKey:#"image"]]placeholderImage:[UIImage imageNamed:NEWSAVATAR]];

This delegate method is called only when that cell is visible i.e you have scrolled to the cell. If you dont want this to happen you have to call a web-service and save all the images locally and then pick it from your local folder.

Related

why images not loading from xml file of url and not showing in table view

Am retrieving XML file from the url and i got the images through parsing it. My XML file consists like this,
<Products>
<products id="1">
<img>http://images.com/images/image1.jpg</img>
</products>
<products id="2">
<img>http://images.com/images/image2.jpg</img>
</products>
<products id="3">
<img>http://images.com/images/image3.jpg</img>
</products>
</Products>
The Tableviewcontroller.m has the code like this,
- (void)viewDidLoad{
[super viewDidLoad];
xmlParser = [[XMLParser alloc] loadXMLByURL:#"http://images.com/Products.xml"];
[self.tableView reloadData];}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [xmlParser.tweets count];}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Products";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];}
UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:1];
cell.imageView.image = currentTweet;
[cell.contentView addSubview:self.productImage];
return cell;}
Here xmlParser is the object of class XMLParser and tweets is the array. The product image is reference of the UIImageview.
Now my problem is when i run the app it shows the image in tableview as a thumbnail picture, but i want to show image as the below screenshot i shown.
Also am confused in referencing UIImage view in storyboard, when i define productImage as UIImageView in .h file and try to connect the storyboard it shows me error like Illegal Configuration: Connection "productImage" cannot have a prototype object as its destination. so kindly tell me how to connect it in storyboard too.
Kindly suggest me to solve this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Products";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];}
UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UIImageView *tweetImageView = [UIImageView alloc]
initWithFrame:CGRectMake(0,0,
cell.frame.size.width , cell.frame.size.height)];
tweetImageView.image = currentTweet;
[cell.contentView addSubview:tweetImageView];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return self.view.frame.size.height/3;
}
You are trying to assign ID type to UIImage
Try this code to load image for you cell
NSURL *imageURL = [NSURL URLWithString:[[xmlParser tweets] objectAtIndex:1]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *currentTweet = [UIImage imageWithData:imageData];
cell.imageView.image = currentTweet;
1) Problem : Getting a thumbnail image in table view but you want to show that image in background (complete cell width) Am i right??
Solutions :
In below mentioned way your image can be spread in background
UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:1];
UIImageView *tweetImageView = [UIImageView alloc]
initWithFrame:CGRectMake(0,0,
cell.frame.size.width , cell.frame.size.height)];
tweetImageView.image = currentTweet;
[cell.contentView addSubview:tweetImageView];
Another option is that you can set the background image of the cell like :
[cell setBackgroundView:tweetImageView]
Update :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UIImageView *tweetImageView = [UIImageView alloc]
initWithFrame:CGRectMake(0,0,
cell.frame.size.width , cell.frame.size.height)];
tweetImageView.image = currentTweet;
[cell.contentView addSubview:tweetImageView];
return cell;
}
Dont create separate imageviews instead use one image view.
And for every row(indexPath.row) pass different image to the imageview.
For every row a new cell gets created an its new imageview will fetch corresponding image from array
Hope it helps
:)

UITableViewController scrolling lag with large amount of images

I have following code of cellForRowAtIndexPathmethod:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"champion cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImage *cellIcon = [UIImage imageNamed:[arrayOfChampionPictures objectAtIndex:indexPath.row]];
[[cell imageView] setImage:cellIcon];
cell.imageView.frame = CGRectMake(0.0f, 0.0f, 70.0f, 70.0f);
cell.imageView.layer.cornerRadius = 7;
[cell.imageView.layer setMasksToBounds:YES];
cell.textLabel.text = [arrayOfChampionNames objectAtIndex:indexPath.row];
return cell;
}
Where arrayOfChampionNames is local database stored in NSMutableArray that contains pictures names. It's about 103 cells with images in my UITableView. It lags at first scroll from beginning to end, after that it's scrolling smoothly. There's no lags on simulator.
Possible solutions I came up with, but i don't know how to realise them
Load images after scrolling,
Preload image data into UITableView.
I'll tell you where the lag is coming from - corner radius. Pre compute the images rounded corners. Doing them dynamically kills scrolling performance.
You forgot something:
static NSString *CellIdentifier = #"champion cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
This way you'll actually utilize the reusable identifier that you created.

UITableViewCell imageView not showing

So I had the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"FriendsCell";
FriendData * fd = [self.friendsDataSource_ objectAtIndex:indexPath.row];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]];
[cell.imageView setFrame:CGRectMake(0, 0, 30, 30)];
[cell.textLabel setText:fd.name_];
return cell;
}
However, I am not seeing the imageView in the cell. What am I doing wrong?
I got the same problem with SDImageCache as well. My solution is to put a placeholder image with the frame size you want.
UIImage *placeholder = [UIImage imageNamed:#"some_image.png"];
[cell.imageView setImage:placeholder];
[cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]];
1) Set Image with URL is not an iOS method. It's something custom and that may be the problem. But until you publish it can't help there.
2) I think cell.imageView will ignore "setFrame". I can't get that to work in any of my table cells using an image. It always seems to default to width of the image.
3) Typically you set the image using cell.imageView.image = your_Image. The ImageView is READONLY and probably the ImageView frame is private.
4) I think you are going to need to create a custom cell of your own.
Usage of a method with placeholder will fix it.
[cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]placeholderImage:[UIImage imageNamed:#"placeholder"]];
you need to return cell at the end of the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"FriendsCell";
FriendData * fd = [self.friendsDataSource_ objectAtIndex:indexPath.row];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]];
[cell.imageView setFrame:CGRectMake(0, 0, 30, 30)];
[cell.textLabel setText:fd.name_];
return cell;
}

How to change the UITableViewCell image on the left on the UIBarButtonItem?

I am a newbie to iPhone programming. I am stuck in a situation. I need to change the UITableViewCell image (on the left side) through calling an action from UIBarButtonItem fromt he UIToolBar. I have searched through internet, but can't find any appropriate solution.
Any Help regarding this would be appreciated.
Here is a sample code for customizing cell imageView.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
[cell.imageView setBounds:CGRectMake(0, 0, 30, 30)];
[cell.imageView setClipsToBounds:YES];
[cell.imageView setFrame:CGRectMake(0, 0, 30, 30)];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
// customize cell
[cell.imageView setImage:**your_image**];
return cell;
}
With the [tableview cellForRowAtIndexPath:] method you can access a cell at a specific row. When you have accessed that you can reach the image by cell.imageView. Have a look at this:
- (IBAction)barbuttonPressed
{
UITableViewCell *cell = [self.tableView [NSIndexPath indexPathForRow:row inSection:section];
cell.imageView = .....
}
Replace row and section with appropriate.

UiTableView bug, added cell doesnt insert text needed

Hey guys i have a table view that can add and delete cells. but when i delete the only cell that comes with the app, and i try to add a new cell with the text of "Meghan Way" and the text just automatically changes it self to the original cells text which is "House 1" here is my code!
- (IBAction)saveButton:(id)sender {
FacePlatesViewController * viewController = (FacePlatesViewController
*)self.parentViewController;
[viewController addObject:[NSMutableDictionary dictionaryWithObject:text.text
forKey:#"name"]];
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)cancel:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [[cells objectAtIndex:indexPath.row] valueForKey:#"name"];
UIImage * myimage = [UIImage imageNamed: #"check.png"];
image = [[UIImageView alloc]initWithImage:myimage];
tableView.backgroundColor = [UIColor clearColor];
self.myTableView = tableView;
}
return cell;
}
this is for the save button! any help would be very appreciated:D Thanks
You code is quite messed up. I have improved it below and added comments:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
} //The if block should end here. You should set the cell's label irrespective whether the cell was nil. This is the cause of the issue you are facing.
cell.textLabel.text = [[cells objectAtIndex:indexPath.row] valueForKey:#"name"];
//You are doing nothing with the UIImage
//UIImage * myimage = [UIImage imageNamed: #"check.png"];
//image = [[UIImageView alloc]initWithImage:myimage];
//You should be setting the background color just once, in ViewDidLoad, not here.
//tableView.backgroundColor = [UIColor clearColor];
//I don't see why this is required
//self.myTableView = tableView;
return cell;
}
Are you storing the text labels for these cells in any kind of array or dictionary and loading them in there using the standard "cellForRowAtIndexPath?" If so, then you need to delete the dictionary / array entry in that data source to prevent it from being used again.
I can't tell for sure without seeing some more code...but it sounds like a cell recycling issue. I could be wrong...but I'd need to see more info.