When I click on my tableView's detail-button... I can manage to get the section number, the row number, and even the cell itself... but why is the actual "detail button" unobtainable? (NSLog always displays "(null)" for the button.)
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
int section = indexPath.section;
int row = indexPath.row;
UITableViewCell *aCell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *detailButton = (UIButton *)[aCell accessoryView];
// Why is detailButton null?
NSLog(#"accessory button tapped for row with index path %d x %d \n cell=(%#) \n button=(%#)", section, row, aCell, detailButton);
}
The accessory view is a UIView not a UIButton. Perhaps casting it to a button throws off the description method. Just a guess.
Your best bet is to access the button within the view. Perhaps something like
UIButton* detailBtn = [[UIButton alloc] init];
for (UIButton* detail in aCell.accessoryView.subviews)
{
detailBtn = detail;
}
This assumes you only have your detail button in the accessory view, picks it out of the subview, and creates your references to it in detailBtn :)
Related
This question already has answers here:
How to know the UITableview row number
(10 answers)
Closed 9 years ago.
I have a UITableView for which I have created a custom UITableViewCell. Each row in tableview has a button. I want to know the section number on click of a button, so that I would know that from which section button has been clicked. I have already tried few things found on stack but nothing is working.
UIButton *b = sender;
NSIndexPath *path = [NSIndexPath indexPathForRow:b.tag inSection:0];
NSLog(#"Row %d - Section : %d", path.row, path.section);
Don't know what you've tried, but i might do something like this. Doing some pseudocode from memory, here.
- (void)buttonClicked:(id)sender {
CGPoint buttonOrigin = [sender frame].origin;
// this converts the coordinate system of the origin from the button's superview to the table view's coordinate system.
CGPoint originInTableView = [self.tableView convertPoint:buttonOrigin fromView:[sender superview];
// gets the row corresponding to the converted point
NSIndexPath rowIndexPath = [self.tableView indexPathForRowAtPoint:originInTableView];
NSInteger section = [rowIndexPath section];
}
If I'm thinking clearly, this gives you flexibility in case the button's not directly inside the UITableView cell. Say, if you've nested inside some intermediary view.
Sadly, there doesn't seem to be an iOS equivalent of NSTableView's rowForView:
Create a handler for button click and add it in tableView:cellForRowAtIndexPath: method
- (void)buttonPressed:(UIButton *)button{
UITableViewCell *cell = button.superView.superView;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//Now you have indexPath of the cell
//do your stuff here
}
When you create your custom UITableViewCell in cellForRowAtIndexPath you should pass it its section as a parameter. It could look like:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (!cell)
{
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier" section:indexPath.section] autorelease];
}
return cell;
}
Now your cell knows its section and you can use it when performing click method in MyCustomCell class
Try this,
First assign section as a tag to button also add target on button in cellForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell.btnSample setTag:indexPath.section];
[cell.btnSample addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
...
}
Get Section as tag from sender of IBAction you defined (buttonClicked here).
-(IBAction)buttonClicked:(id)sender
{
NSLog(#"Section: %d",[sender tag]);
}
I have UIButton which is placed on top of each UITableviewCell . It is custom tableview . When i click on tableviewcell it goes into another tableview where (Tableview) contains the data in each cell .
This is the way i get the details of the 2nd tableview ....customCellData is the db query method which gives out the cell number ..(Ex if i click 2nd cell of first tableview customcell returns the value )
(bookArray - contains the list of items present in 2nd cell of first tableviewcell)
The following code is didSelectForRowAtIndexPath
NSDictionary *selectedAuthor = nil;
NSArray *sectionArray=[mainIndexDictionary objectForKey:[allKeysArray objectAtIndex:indexPath.section]];
selectedAuthor = [sectionArray objectAtIndex:indexPath.row];
iPath=[[selectedAuthor objectForKey:#"SymptIndexID"] intValue];
NSLog(#"iPath - %d",iPath);
authortitleString=[[selectedAuthor objectForKey:#"authorIndexName"]stringByAppendingString:#" cure!"];
}
bookArray=[objDB customCellData:(NSInteger)iPath];
[self.view bringSubviewToFront:authorView];
[bookTableView scrollRectToVisible:CGRectMake(0.0, 0.0, 320.0,10.0) animated:NO];
[bookTableView reloadData]
Now i want to program in such a way where .when i click on the UIButton which is there in 1st tableview it should contain the data of the 2nd tableview . I cant access indexPath since i will be doing this in seperate method ( In didselectrowatindexpath if i give indexpath for that particular cell the indexpath is found )
Will anyone please help me .
Updated Code
in cellForRowAtIndexPath
cell.shareFacebookButton.tag = indexPath.row;
[cell.shareFacebookButton addTarget:self action:#selector(facebookShare:) forControlEvents:UIControlEventTouchUpInside];
return cell;
-(IBAction)facebookShare:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
NSInteger indexPathNumber = indexPath.row;
NSLog(#"IndexPath is -- > %d" , indexPathNumber);
}
Answer Updated
Ok, Below is the function you need to use for custom cell buttons
"cellForRowAtIndexPath" or use any other given appropriate function.
And to identify which button was clicked, do as below
<buttonid>.tag = <indexPath>.row;
Regards,
Ravi
I have a table view wherein the number cells are not fixed and will keep on changing. I have to show two action buttons after my last cell in the table footer. How can I place them properly after my last cell? I know the pixel spacing between last cell and these buttons. Any code sample will be really helpful.
Have you tried sticking them in a view and setting that as the footer view?
You need to give it the correct height before you add it; the width is automatically set to the width of the table. Either set it to a sensible width (e.g. 320) and use autoresizing or use a custom UIView subclass and implement -layoutSubviews.
You could always add two buttons to the final cell.
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
return [myCellDataArray count]+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.row > [myCellDataArray count]) {
cell = [tableView dequeueReusableCellWithIdentifier:#"Button Cell"];
if (cell == nil) {
UIButton *firstButton = [[UIButton alloc] init];
//customization
UIButton *secondButton = [[UIButton alloc] init];
//customization
[cell addSubView:firstButton];
[cell addSubView:firstButton];
}
} else {
// normal stuff
}
If you want to customize existing buttons you need to set it's tag to something unique, i.e. firstButton.tag = 100 and then set firstButton by firstButton = (UIButton *)[cell viewWithTag:100];. Make sure you define firstButton so that it's in scope!
Hope this helps!
I create tow or more custom cell
each cell has a switch inside
How can I know which switch in the row I click
ex.I click the switch in row 3,than It will return indexPath.row = 3
and also the switch status is on or off ?
which void I should put in ?
I know there is a way can get indexpath return by:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
But I don't want to touch the row,just the switch ...
Oh I do some research but it just be a little different
How can keep track of the index path of a button in a tableview cell?
any ideas ?
I wish I can post my screen shot,but not I am a new user
the system doesn't allow me to do this
When you create the switch, set up a target and action for it:
[mySwitch addTarget:self action:#selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
Here, the target self is your UITableViewController, in which you'll need to implement the method (void) switchToggled:(id)sender to handle the control event. The switch will automatically send itself to the switchToggled method as the sender when it is toggled. Like this (modified from the UIButton example you linked):
- (void)switchToggled:(id)sender {
UISwitch *theSwitch = (UISwitch *)sender;
UITableViewCell *cell = (UITableViewCell *)theSwitch.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
if(theSwitch.on) {
// switch turned on
}
else {
// switch turned off
}
}
When button is created
button.tag = [indexPath row]
When button is selected, pull the row out of the tag
int myRow = [(UIButton *)sender tag];
How can i get a table view in which whn i click on a cell a text view should expand just below that cell it should look like clicking on a button an it will create a text view in between clicked button and its below button...
i m trying to create a pictorial view...
tabel view:
button 1 >
button 2 >
button 3 >
table view:
button 1 >
button 2 v
__________
.
.
. text view
.
----------
button 3 >
plz solve my problem i m new for iphone app development....
I dont think that this can be done the way u want it, because once the TableView is loaded it is loaded, and if even if you manage to push some extra cells in between, the Table View will not change until it is loaded again.
TableView is loaded in two scenarios i.e
1. When Loading the view
2. While scrolling within the TableView.
my advice is to show a popup window kind of thing...
- (UITableViewCell *)valueForSelectedRow {
UITableViewCell *cell = [self.AccountTable cellForRowAtIndexPath:[self.AccountTable indexPathForSelectedRow]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Get value
UITableViewCell *cell = [self valueForSelectedRow];
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
UITextview *txtview = [[UITextView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 25.0f)] autorelease];
[cell addSubview:txtview];
[cell sizeToFit];
//...
....
You might insert a row just under the selected one.
The steps are:
Create an instance of NSIndexPath for the new row
Update your datasource (insert a special object containing the information for your text view). Notice that the datasource object must be mutable (e.g. NSMutableArray).
Insert new row to the table
Sample implementation may look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// 1
NSIndexPath *newRowIndexPath = [NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:indexPath.section];
// 2 (datasource has to be mutable)
[self.datasource insertObject:[NSDictionary dictionaryWithObject:#"THE_TEXT_FOR_YOUR_TEXTVIEW"] atIndex:newRowIndexPath.row];
// 3
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newRowIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
Notice that in addition to this code you will have to treat the "special" row differently in tableView:cellForRowAtIndexPath: method...
Cheers.
Michael.