I have custom cell in iPhone which have which have a button and image view i want to change the image of image view when i clicked on the button here is my button code
-(void)btnLikePressed:(UIButton *)sender{
imageDC *objMenu = [dataArray objectAtIndex:sender.tag];
GET_DBHANDLER
[dbHandler performSelector:#selector(like_image:) withObject: objMenu.image_id];
}
To your method to find out the exact index of your UITableViewCell please use the following :-
CGPoint point = [sender convertPoint:CGPointZero toView:_iTableView];
NSIndexPath *indexPath = [_iTableView indexPathForRowAtPoint:point];
You will get your indexpath. From here you can identify your UIImageView, hope you have given tags to them and then change the image.
try this bellow code
-(void)btnLikePressed:(UIButton *)sender{
static NSString *CellIdentifier = #"yourCellName";
UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];
UITableView *tableView=(UITableView*)[cell superview];
NSIndexPath *path=[tableView indexPathForCell:cell];
yourCellName *selectedCell = (yourCellName*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[selectedCell.imageView.image setImage:[UIImage imageNamed:#"yourImageName.png"]];
}
First give tagValue to your imageView , like you gave 5 to your imageView.
Then access this on buttonClick.
-(void)btnLikePressed:(UIButton *)sender
{
UITableViewCell *cell=(UITableViewCell*)[sender superview];
UIImageView*imageView=(UIImageView*)[cell viewWithTag:5];
//Now do whatEver you want to do with your imageview
}
EDIT
If you added your imageView as
[cell.contentView addSubview:imageView];
Then get your tableView cell like this
UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];
Following steps will work:-
tag your imageview.
use this code in cellForRowatIndexpath
NSArray *nib;
static NSString *cellIdentifier= #"cell";
UITableViewCell *theCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(theCell== nil)
{
{
nib = [[NSBundle mainBundle] loadNibNamed:<Your cellNibName> owner:self options:nil];
}
theCell = [nib objectAtIndex:0];
theCell.selectionStyle = UITableViewCellSelectionStyleNone;
// theCell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
UIImageView *imageView=(UIImageView*)[[theCell contentView] viewWithTag:<Your tag>];
//do what ever you want with imageView
You got image view ref do what you want.
In UITableViewDataSource function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
while setting cell just add tag to your button and imageView as well.
cell.yourBtn.tag = indexPath.row;
cell.imageView.tag = indexPath.row + 1000(any number);
now in your UITableViewController class
now when user click on the button you get the button tag that is actually your row number and imageView(btn tag + 1000) tag as well that you can get imageView by using same info.
-(void)btnLikePressed:(UIButton *)sender{
NSInteger btnTag = [sender tag];
UIImageView *imageView = [yourTableView viewWithTag:btnTag + 1000];
// here your code by using imageView
}
Related
in my cellForRowAtIndexPath
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//...do cell setup etc.
UIImage *iconthumbNail = [UIImage imageNamed:#"icon.png"];
UIImageView * iconimgView = [[UIImageView alloc] initWithFrame:CGRectMake(265, 34, 25, 25)];
[iconimgView setImage:iconthumbNail];
//imgView.image = thumbNail;
[cell addSubview:iconimgView];
[iconimgView release];
// add a few more UIImageViews to the cell
}
So then in my
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell * theCell = [tableView cellForRowAtIndexPath:indexPath];
for (UIView * b in cell.subviews)
{
if ([b isKindOfClass:[UIImageView class]])
{
// how do I check that its 'iconImgView' so that I can change it?
}
}
So I add an UIImageView in my cellForRowAtIndexPath, then when that table cell is selected I want to change one of the images in the cell, 'iconImgView' but how do I identify this UIImageView from the other UIImageViews present in that cell?
Many Thanks,
-Code
Instead of using that approach, I would assign a tag to the UIImageView you are looking for.
iconimgView.tag = 1;
Then in the didSelectRowAtIndexPath method, use this:
UIImageView *iconimgView = (UIImageView *)[cell viewWithTag:1];
If you still want to do it your way, you can do like this:
if ([b isKindOfClass:[UIImageView class]]) {
UIImageView *myView = (UIImageView *)b;
if ([b.image isEqual:[UIImage imageNamed:#"yourImageName"]]) {
// Do your stuff
}
}
If your only goal is to change the image of the selected cell, this can be done more efficiently. All you have to do is read/write to the cells imageView property.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath];
[[theCell imageView] setImage:[UIImage imageNamed:#"someImage.jpg"]];//altering the existing image
UIImageView *myImageView = theCell.imageView;//read the image property of the cell
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I am doing customized cell for uitable view like below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"overViewCell";
tableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( tableCell == nil ) {
[[NSBundle mainBundle] loadNibNamed:#"OverViewCell" owner:self options:nil];
tableCell = overViewCell;
self.overViewCell = nil;
}
..............................................................
}
and OverViewCell.xib looks like ( the right image is tagged as 3 )
And later, I would like to change the image of particular cell from the table view by doing this
- (void) updateImageView {
// currentRow is getting value when cellForRowAtIndex is clicked
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:currentRow inSection:0];
UITableViewCell *cell = [self.tableContent cellForRowAtIndexPath:indexPath];
imageView = (UIImageView*)[cell viewWithTag:3];
imageView.image = [UIImage imageNamed:#"checked.png"];
[self dismissViewControllerAnimated:YES completion:nil];
}
However, the image is not changed at all
What I am missing in the middle... Please help if you have any clues about this issue.
I figured what the problem is. it is because I was assigning the wrong tag to that image
I am new to iphone.I have a small doubt (i.e),I have create a table view in that i am placing all my book names and download option to that particular book in each cell like as below
Genesis Download
Exodus Download
Leviticus Download
Here is the above Genesis,Exodus,Leviticus are booknames and download is the button for download that book like this i have 66 different books in table view.Here my question is when we click on download button i want to get the corresponding bookname of that tableview cell.
My code is like below
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 66;
}
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIButton *downloadButton = nil;
//this is the custom cell i have created one class for this in that i am place the string titlelabel.
CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
downloadButton.frame = CGRectMake(220,10,50,30);
[downloadButton setImage:[UIImage imageNamed:#"download.png"] forState:UIControlStateNormal];
[downloadButton addTarget:self action:#selector(downloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
downloadButton.backgroundColor = [UIColor clearColor];
downloadButton.userInteractionEnabled = YES;
downloadButton.highlighted = YES;
[cell.contentView addSubview:downloadButton];
}
NSString *titleLabel = [[appDelegate getBookNames]objectAtIndex:indexPath.row];
cell.TitleLabel.text = titleLabel;
return cell;
}
-(void)downloadButtonClicked:(id)sender{
}
In the button action you can get the cell by accessing the superView
-(void)downloadButtonClicked:(id)sender{
UIButton *button = (UIButton*)sender;
UIView *view = button.superview; //Cell contentView
UITableViewCell *cell = (UITableViewCell *)view.superview;
cell.textLabel.text; //Cell Text
}
first set the tag of your label say it is 100.
//in the button click method...
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
UILabel *lbl = (UILabel *)[cell viewWithTag:100];//this is for custom label
NSLog(#"label text =%#",lbl.text);
else NSLog(#"label text =%#",cell.titleLabel.text);
When you tap on the table view cell the Delegate function will be called and it will give you the index path section and row value . When you putting button on table cells just allocate the tag - indexpath.row to the button tag. Find this tag when you call the button function and find the aray index to get the value for that index on the array.
I am new to iphone.I have a small question that is,I have a class CustomCell in that i have declare all elements which i have required like the below
// CustomCell.h
here in this class i am setting the getter and setter methods to TitleLabel, PercentageLabel, ProgressView, downloadButton
// CustomCell.m
here in this class set the frame to above all,and set the image to the download button also here
screen shot of my output screen is
// In my table view i have a download button when we click that the following method will executes
-(void)downloadButtonClicked:(id)sender
{
NSLog(#"download button clicked");
int index = [sender tag];
NSLog(#"index of the cell is %d",index);
UIButton *button = (UIButton*)sender;
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
NSLog(#"label text =%#",titleLabel.text);
UIView *senderButton = (UIView*) sender;
NSIndexPath *indexPath = [tableView indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]];
NSLog(#"indexpath is %d",indexPath.row);
CustomCell *customCell = [[CustomCell alloc]init];
progressView = [[UIProgressView alloc]init];
// [customCell.contentView insertSubview:progressView atIndex:indexPath.row];
[customCell.contentView addSubview:progressView];
selectedBookTitle = titleLabel.text;
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSMutableArray *allDownloadLinks;
biblePlayerViewController = [[BiblePlayerViewController alloc]init];
allDownloadLinks = [biblePlayerViewController allDownloadLinks];
NSLog(#"all Download Links are %#",allDownloadLinks);
biblePlayerViewController.indexOfSelectedBookTitle = [[appDelegate getBookNames]indexOfObject:selectedBookTitle];
Download* download = [Download downloadWithTitle:selectedBookTitle url:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.audiotreasure.com/%#.zip",[allDownloadLinks objectAtIndex:(biblePlayerViewController.indexOfSelectedBookTitle)]]]PathtoSave:documentsPath];
[[DownloadManager sharedDownloadManager] queueDownload: download];
}
// In cellForRowAtIndexPath tableview delegate method I have written code like this
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSLog(#"indexpath.row is %d",indexPath.row);
NSLog(#"downloadmanager is %d",[[[DownloadManager sharedDownloadManager]getDownloads]count]);
/* if (indexPath.row<[[[DownloadManager sharedDownloadManager] getDownloads]count])
{
cell.TitleLabel.text = ((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).title_;
cell.ProgressView.progress = (float)(((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).PercentageDownloaded_ )/100.0f;
cell.PercentageLabel.text = [NSString stringWithFormat:#"%d %%",((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).PercentageDownloaded_];
}
else
{
[tableView reloadData];
}*/
NSString *titleLabel = [[appDelegate getBookNames]objectAtIndex:indexPath.row];
cell.TitleLabel.text = titleLabel;
cell.downloadButton.tag = indexPath.row;
NSLog(#"tag is %d",cell.downloadButton.tag);
[cell.downloadButton addTarget:self action:#selector(downloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
My question is I have 66 cells in tableview in screen shot we have a bookname called Revelation.It has a 66th cell in my tableview (index is 65).Here when i click the download button in that cell,I have to show progress view in that particular cell only (65th cell).I have reload the table view for every 1 sec also because after placing the progress view we have to show the progress in that progress view in that cell.So, how can we place a progress view in that particular cell index.
Why don't you add the progress view in the cell from the beginning?
have it initially hidden and when you touch the download button you tell the cell to show it.
i see that you store the indexPath.row of the cell in the download button tag. You could use that to find out what cell was selected something like below.
NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:nowIndex];
[cell showProgressBar];
i wrote the code of top of my head..didn't tested it.
I subclassed UITableViewCell and added a button to it. How do I response to the event when the button is touched? I also need to know which index path was the button pressed on.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomTableCell";
CustomTableCell *cell = (CustomTableCell *)
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"CustomTableCell"
owner:nil options:nil];
for (id currentObjects in topLevelObjects){
if ([currentObjects isKindOfClass:[StatusTableCell class]]){
cell = (StatusTableCell *) currentObjects;
break;
}
}
}
cell.customButton.tag = indexPath.row;
return cell;
}
If you have added the UIButton by code in your UITableViewCell Subclass you can use the following code to add an action for it.
//In UITableViewCell subclass
[customButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
And then you have already written the code for accessing the row number by setting,
cell.customButton.tag = indexPath.row;
You can get the tag value in the aMethod and access the indexPath.row value like below,
//In UITableViewCell subclass
-(IBAction)aMethod:(UIButton*)button
{
NSLog(#"indexPath.row value : %d", button.tag);
}
You can assign the IBAction in the CustomTableCell Nib and in the function do something like this:
- (IBAction) btnDoSomethingPressed:(id)sender {
NSIndexPath *indexPath = [yourTableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
//Use indexPath (or indexPath.row) like you would in a UITableViewDelegate method
}
This assumes that yourTableView is an instance variable assigned to your table.