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.
Related
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
}
I want to have a custom cell in my UITableview. I have tried it with the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell*)view;
}
}
}
[cell setLabelText:[daten objectAtIndex:indexPath.row]];
return cell;
}
But I am getting an exception on that line:
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
I'm almost sure that you have lost connection in your XIB file. If commentLabel is still exists you should either delete it and create a new outlet or check the outlets section and delete extra connection and all will be alright!!
EDIT:
This error occurs when you have two outlets connected to the same label in IB.
This is how I made my custom cell and called it from the class :-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[cell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = customCell;
customCell = nil;
}
// Configure the cell.
cell.serialLabel.text = [[NSNumber numberWithInt:indexPath.row+1]stringValue];
cell.textLabel.textColor=[UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
if u are not using the latest ios SDK, make sure that u have written
#synthesize commentLabel;
in CustomCell
Try cleaning the build folder, or try Simulator -> Reset Content and Settings.
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
In my app I have custom UITableViewCell, and I have UIStepper and UILabel in the custom cell. I don't know how to check which stepper was clicked. So is it a way to know from which cell stepper was clicked?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
if ([nib count] > 0) {
cell = self.tbcell;
}
}
return cell;
}
An alternative is:
In the method that gets fired when the UIStepper value changes (e.g. #max_, above), you can do the following:
- (IBAction)stepperValueDidChanged:(UIStepper *)sender {
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
// assuming your view controller is a subclass of UITableViewController, for example.
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
[step addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventValueChanged];
- (void) someAction:(UIStepper *) stepper {
NSLog(#"stepper clicked");
}
I have TableCellViewController for managing cells in my UITableView. Each cell has a label na UISwitch (dictTypeSwitch). A want to assign method to switch events so I can save the state of button.
So far I've done this:
assign setState function to object:
[cell.dictTypeSwitch addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchUpInside];
function for handling event:
- (void)setState:(id)sender {
BOOL state = [sender isOn];
NSString *rez = state == YES ? #"YES" : #"NO";
NSLog(rez);
}
From sender I get UISwitch object from which I can get state.
So far everything is ok.
But if I want to save the state of UISwitch, I have to get rowIndex for this cell.
How can I achieve this?
The cells are made inside function cellForRowAtIndexPath. Se the code bellow:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"DictionariesTableCellViewController";
DictionariesTableCellViewController *cell = (DictionariesTableCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DictionariesTableCellViewController" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (DictionariesTableCellViewController *) currentObject;
break;
}
}
}
// Configure the cell...
cell.dictTypeLabel.text = [NSString stringWithFormat:#"row - %i",indexPath.row];
[cell.dictTypeSwitch addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Thanks
You can use the UISwitch's .tag property to store an arbitrary integer. This can be set to the row index.
If it is possible to find the UITableViewCell from UISwitch's parent, you could get the index path with
NSIndexPath* indexPath = [theTableView indexPathForCell:theCell];