Cancel Selector in UITableViewCell - iphone

I have scenario that in which i am using selector in TableViewCell as bellow and when I click on back button in my view I want to cancel that selector and I am sending dictionary as object in Selector
My code is as bellow
In Header File
NSMutableDictionary* photoDict;
NSMutableDictionary* dictImages;
In .M file
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
[[NSBundle mainBundle]loadNibNamed:#"MyCellNib" owner:self options:Nil];
cell = detailChainObj;
}
NSString* avatarURL =#"image_Url"; //Any url of image for cell.
NSString *key =[[NSString alloc] initWithFormat:#"Key%d%d",indexPath.section,indexPath.row];
dictImages = [NSDictionary dictionaryWithObjectsAndKeys:imageViewCell,#"Img",imgURL,#"imgURL",key,#"key", nil];
[self performSelectorInBackground:#selector(DownloadLinkzImageOfUser:) withObject:dictImages];
if([photoDic valueForKey:keyAvt])
{
NSData* data = (NSData*)[photoDic valueForKey:key];
imageViewCell.image = [UIImage imageWithData:data];
}
else
{
[self performSelectorInBackground:#selector(DownloadImagesForCell:) withObject:dictImages];
}
}
//
-(void)DownloadImagesForCell:(NSDictionary *)result
{
UIImageView* img = (UIImageView*)[result objectForKey:#"Img"];
NSString* urlAvt = [result valueForKey:#"imgURL"];
if([urlAvt isEqualToString:#"No Image"])
{
img.image = [UIImage imageNamed:#"noimg.png"];
}
else
{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];
img.image = [UIImage imageWithData:data];
[photoDic setValue:data forKey:[NSString stringWithFormat:#"%#",[result valueForKey:#"key"]]];
}
}
Now I want to cancel this selecter when I press back button
and please keep in mind that I have already use
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(DownloadImagesForCell:) object:dictImages];

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html
I guess this method works for performSelector:withObject:afterDelay: only.

Related

Fetching and displaying facebook profile picture in a UIImage

I'm trying to make a facebook friend picker displaying the friends who currently have the app installed that are also facebook friends with the current user.
Here I'm fetching the user's facebook profile picture but, I seem to have a warning where the last statement is not used. Thanks for your help.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"friendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *facebookID = [[PFUser alloc] init];
facebookID = [friendUsers objectAtIndex:0];
NSString *fbIdString = [[NSString alloc] init];
fbIdString = [facebookID objectForKey:#"fbId"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSURL *profilePictureURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large", fbIdString]];
NSData *picData = [NSData dataWithContentsOfURL:profilePictureURL];
UIImage *fbPic = (UIImage *)[self.view viewWithTag:1001];
[fbPic initWithData:picData];
}
// Configure the cell...
return cell;
}
You need to actually assign the result of the initWithData: call to your fbPic image.
E.g.
fbPic = [fbPic initWithData: picData];
From the apple documentation on UIImage:
- (id)initWithData:(NSData *)data
Parameters
data -
The data object containing the image data.
Return Value
An initialized UIImage object, or nil if the method could not initialize the image from the specified data.
A couple of things:
UIImage *fbPic = (UIImage *)[self.view viewWithTag:1001]; - I think you're trying to get the UIImageView from the cell?
There's no UIImageView where you're actually showing the image.
It should be something like this:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSURL *profilePictureURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large", fbIdString]];
NSData *picData = [NSData dataWithContentsOfURL:profilePictureURL];
UIImageView *fbPicImageView = (UIImageView *)[self.view viewWithTag:1001];
[fbPicImageView setImage:[UIImage imageWithData:picData]];
}

Image lazy loading in table view

I want to use lazy loading of images concept in my table view. My array of image rul string is ( MSMutableArray ) "images". How can I implement this in cellForRowAtIndexPath.
It looks like:
-(UITableViewCell *) tableView:(UITableView *)AtableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[searchtable dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
cell.textLabel.font=[UIFont systemFontOfSize:12];
cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[appSmallLogos objectAtIndex:indexPath.row]]]];
cell.detailTextLabel.text=[appReleaseDates objectAtIndex:indexPath.row];
}
return cell;
}
Try this code :
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(your bounds);
[cell addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[indicator startAnimating];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
//This is what you will load lazily
NSString *u=[NSString stringWithContentsOfFile:r.url encoding:NSUTF8StringEncoding error:nil];
NSURL *imageURL=url;
NSData *image=[NSData dataWithContentsOfURL:imageURL];
dispatch_sync(dispatch_get_main_queue(), ^{
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image=[UIImage imageWithData:image];
[cell setNeedsLayout];
[indicator stopAnimating];
});
});
cell.cellDescription.text=r.url;// this is which you want to add in first time.
I am not sure but try it.
You can use the SDWebImage library, the library will download and cache the image asynchronously. by passing the url to it as argument
- (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;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TableCellLayout *cell = (TableCellLayout *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[TableCellLayout alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.cellName = [[array valueForKey:#"name"] objectAtIndex:indexPath.row];
cell.cellImage = [UIImage imageNamed:#"placeholder.png"]; // add a placeholder
NSString *imageURL = [[array valueForKey:#"imageLink"] objectAtIndex:indexPath.row];
NSURL *theURL = [NSURL URLWithString:imageURL];
if (asynchronousImageLoader == nil){
asynchronousImageLoader = [[AsynchronousImages alloc] init];
}
[asynchronousImageLoader loadImageFromURL:theURL];
cell.cellImage = asynchronousImageLoader.image;
return cell;
}
I finally managed to do it setting the image with:
– performSelectorOnMainThread:withObject:waitUntilDone:
After the image is downloaded

iPhone/iPad : table view scroll crash in ipad

Hi all I want to show more than 120 images in tableview its runs fine in simulator but crash in device (ipad).
logs show memory warning but I am releasing perfectly I don't know why I get this message and crash. Images comes from server I am using following code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
FullTableViewCell *cell = (FullTableViewCell *)
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"FullTableViewCell" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (FullTableViewCell *)currentObject;
break;
}
}
}
NSDictionary *dic = [socketConnection().imageInfoArray objectAtIndex:indexPath.row];
int imageId = [[dic objectForKey:#"id"] intValue];
UIImage *image = [socketConnection().serverImageArray objectAtIndex:indexPath.row];
UIImage *IMMage = [self scaleImage:image toResolution:740];
cell.cellIMage.image = IMMage;
cell.addButton.tag = imageId;
cell.zoomOutButton.tag = imageId;
[cell.zoomOutButton addTarget:self action:#selector(_ZoomOut:) forControlEvents:UIControlEventTouchUpInside];
[cell.addButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
cell.transform = CGAffineTransformMakeRotation(3.14159265+(3.14159265/2));
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
plz anybody help me where i am wrong..
Implement:
if (cell) {
[cell setImage:nil];
[cell setImage://image];
// Put ALL cell code here
}
and release:
image and IMMage and FullTableView and topLevelObjects and dic before
return cell;
AND FINALLY AND MOST IMPORTANTLY:
also analyze you're project to see for leaks

how to set image in uitable view

I download some images using an NSThread. When all the images are downloaded, I have to put them in cell.myimageview. Give me the solution for setting an image in a user-defined method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableCell *cell = (TableCell *)[TableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSString *bedsbaths=[NSString stringWithFormat:#"Beds:%# Baths:%#",[[AppDeleget.statuses valueForKey:#"beds"] objectAtIndex:indexPath.row],[[AppDeleget.statuses valueForKey:#"baths"] objectAtIndex:indexPath.row]];
cell.mlsno.text=[[AppDeleget.statuses valueForKey:#"mlsno"] objectAtIndex:indexPath.row];
cell.price.text=[[AppDeleget.statuses valueForKey:#"price"] objectAtIndex:indexPath.row];
cell.address.text=[[AppDeleget.statuses valueForKey:#"address"] objectAtIndex:indexPath.row];
cell.bedsbaths.text=bedsbaths;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
-(void)LoadImage
{
for(int x=0;x<[ListPhotos count];x++)
{
NSData *imageData =[ListPhotos objectAtIndex:x];
id path = imageData;
NSURL *url = [NSURL URLWithString:path];
NSLog(#"%#",url);
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[self performSelectorOnMainThread:#selector(downloadDone:) withObject:img waitUntilDone:NO];
}
}
-(void)downloadDone:(UIImage*)img {
// I have to set the cell here. How?
cell.myimageView.image=img
}
In -(void)LoadImage
Store the img in a NSArray.
In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath , code as
cell.myimageView.image=[imgArray objectAtIndex:indexPath.row];
Assign the delegate and datasource of tableview as,
-(void)downloadDone:(UIImage*)img {
self.tableView.delegate=self;
self.tableView.datasource=self;
}

Loading an image into UITableViewCell from an NSArray

I have several hunderd .jpgs in /Resources within an iOS project.
Here is the viewDidLoad method:
- (void)viewDidLoad {
NSArray *allPosters = [[NSBundle mainBundle] pathsForResourcesOfType:#"jpg" inDirectory:#"."];
[super viewDidLoad];
}
The above successfully loads all of the .jpgs into an NSArray.
I simply need to display all of the .jpgs within this array in UITableViewCells in a UITableView
Here is the -(UITableViewCell *)tableView:cellForRowAtIndexPath: method:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
NSDictionary *posterDict = [allPosters objectAtIndex:indexPath.row];
NSString *pathToPoster= [posterDict objectForKey:#"image"];
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
UIImage *theImage = [UIImage imageNamed:pathToPoster];
cell.ImageView.image = [allPosters objectAtIndex:indexPath.row];
return cell;
}
I know the issue is with cell.ImageView.image, but am not sure what the issue is? How can I grab each .jpg from the array and display in each row?
NSArray *allPosters = [[NSBundle mainBundle] pathsForResourcesOfType:#"jpg" inDirectory:#"."];
This will give you an array of paths (as suggested by the method name). Those paths are NSStrings.
But you are assigning this array to a local variable, and this variable will be gone after you left viewDidLoad.
so you have to change it into something like this:
allPosters = [[[NSBundle mainBundle] pathsForResourcesOfType:#"jpg" inDirectory:#"."] retain];
Another one:
NSDictionary *posterDict = [allPosters objectAtIndex:indexPath.row];
NSString *pathToPoster= [posterDict objectForKey:#"image"];
And this would definitely crash if you would have assigned the array correctly.
Change it into
NSString *pathToPoster = [allPosters objectAtIndex:indexPath.row];
Next one:
UIImage *theImage = [UIImage imageNamed:pathToPoster];
cell.ImageView.image = [allPosters objectAtIndex:indexPath.row];
UIImages imageNamed: doesn't work with paths, it needs filenames. And of course you want to assign the real image to the imageview and not the path to the poster. So change it:
UIImage *theImage = [UIImage imageNamed:[pathToPoster lastPathComponent]];
cell.imageView.image = theImage;
Try using [UIImage imageWithContentsOfFile:pathToPoster] rather than imageNamed. And set the value to the UIImage object.
It's probably just a typo, but it should be:
//lowercase "i" in imageView
cell.imageView.image = [allPosters objectAtIndex:indexPath.row];
Also- you are creating UIImage *theImage, but then you don't use it. What's going on there?