I want to change a UIButton text to the selected cell in a UITableView. Normally this would be done like this.
TableView.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if([self.delegate respondsToSelector:#selector(ButtonText:)]) {
[self.delegate ButtonText:selCell.textLabel.text];
}
}
ViewController.m:
- (void)dateSpecifiedButtonText:strText2
{
[self.dateSpecifiedButton setTitle:strText2 forState:UIControlStateNormal];
}
This is working when I select the cell but what I want is to change the button to the cell when you click on another button. So I need to rewrite this method without using didSelectRowAtIndex but I have no idea how.
If you know the indexPath of the cell in which button is
UITableViewCell *cell = [self.tableView cellForIndexPath:indexPath];
If you have just added button to contentView of cell give a unique tag to the button
UIButton *button = (UIButton *)[cell.contentView viewForTag:55];
Now you have the instance of button, set the text to it.
Related
I taken textfield and edit button within tableView cell when i press particular cell button i shown the cursor and button image changed and i called api on this. If i click button again cursor are not showing in textfield.
Try this: Assign tag to your edit button in cell for row at IndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell.editButton.tag = indexpath.row;
}
get reference to your cell here based on its indexpath. Make its textfield as first Responder.
- (IBAction)editButtonClicked:(id)sender{
UIButton *button = (UIButton*)sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:button.tag inSection:0];
CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell.yourTextField becomeFirstResponder];
}
I am displaying a TabelCell that has images, button, UIView. When click the button it has to show UIView.
UIView is displaying but the problem is, if I click the button in first cell instead of displaying the UIView in the first cell, it is displaying the second cell or in some other cell. I don't know why this is happening.
The code is given below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"HomeTablecell";
cell = (HomeTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.vEdit.hidden=TRUE; //UIView hidden initially
[cell.editButton addTarget:self action:#selector(EditButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //When this button is clicked cell.vEdit become visible
}
-(void)EditButtonClicked:(UIButton*)sender
{
cell.vEdit.hidden=FALSE;
}
The answer for my question is FPPopover, when I use that control, the view is displaying exactly in the position I wanted
https://github.com/50pixels/FPPopover/
1: make sure the cell is referenced correctly(for instance I added tag to the button clicked)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"HomeTablecell";
cell = (HomeTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.vEdit.hidden=YES; //UIView hidden initially
cell.editBUtton.tag = indexPath.row //suppose has 1 section, you can customise the tag rule based on section/row
[cell.editButton addTarget:self action:#selector(EditButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //When this button is clicked cell.vEdit become visible
}
-(void)EditButtonClicked:(UIButton*)sender
{
HomeTableCell* theCell = self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0];
theCell.vEdit.hidden =NO;
}
2: Make sure UIView is hidden in your customised cell class for reused cell
//Your cell class
- (void)prepareForReuse {
[super prepareForReuse];
self.vEdit.hidden=YES;
}
When I select one button in my UItableView other buttons lower down are also clicked. I am using target action on a button in a custom tableview and changing the title of sender in the method. Why is this clicking one button selecting multiple UIButtons? Any help/suggestions would be greatly appreciated.
EDIT: I am using a sectioned tableview
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// Configure the cell...
[cell.likeButton addTarget:self action:#selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
cell.likeButton.tag = indexPath.section;
return cell;
}
-(void)likeButtonPressed:(UIButton *)sender {
NSLog(#"%d",sender.tag);
[sender setTitle:#"Pressed" forState:UIControlStateNormal];
}
In your PostTableViewCell.h define a Delegate
#protocol HandleCellInteractionDelegate <NSObject>
-(void) didPressButton:(NSString*)action forCell:(UITableViewCell *)theCell;
#end
In TableView.h register as a Delegate for this protocol
<HandleCellInteractionDelegate>
In TableView.m file add the delegate handler
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Create cell
....
cell.delegate = self;
if (self.qObjects current object's selection state == selected)
enable button
else
disable button
}
Add the function for protocol
#pragma mark HandleCellInteractionDelegate
-(void) didPressButton:(NSString*)action forCell:(UITableViewCell *)theCell{
// Now here you have access to your cell again. And you can update the cell's button text as needed
// Here save selection state in data model.. in self.qObjects <==
cell.questionTextView.text = #"bla bla";
}
In your uitableviewcell subclass, make sure you are only adding a button to the cell during init. Otherwise you will add an additional button to the cell each time the tableviewcontroller calls dequeueReusableCellWithIdentifier.
Try This:
// Add a property to your custom cell to hold the indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// Configure the cell...
cell.indexPath = indexPath;
return cell;
}
And in your cell subclass wire up an action method, something like this:
- (IBAction)buttonAction:(id)sender {
NSLog(#"indexPath: %#", self.indexPath);
}
i think you have Multiple Rows in a section so all button of that section get selected
use
cell.likeButton.tag = indexPath.row;
instead of
cell.likeButton.tag = indexPath.section;
I have a UIButton in each of my UITableViewCells. When the button is pressed I am disabling it so that the user cannot press it again (it is a like button). However, when the user scrolls passed a cell and the scrolls back to the cell the button is selectable once again. I'm guessing this is because the cell is redrawn when the user comes back to it, resetting the button. Is there a way I can avoid this? My code is below
Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifer = #"cellName";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
}
UIButton *ilikeit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[ilikeit addTarget:self action:#selector(like:) forControlEvents:UIControlEventTouchUpInside];
ilikeit.frame = CGRectMake(55, h+70, 45, 25);
[cell addSubview:ilikeit];
return cell;
}
-(void) like:(id) sender {
((UIButton *)sender).enabled = NO;
}
You can store the button state in your data model for the tableview class.
Say the TableView is loading data from a class MyData which looks like this:
#interface MyData: NSObject
// Your other data here such as strings etc
NSString *otherData;
// Here add a selected flag
BOOL selected;
#end
in the TableViewDelegate, read and write to this model.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyData *thisCellData = [yourGlobalMyDataArray objectAtIndex:indexPath.row];
// set other data
if (thisCellData.selected){
// Hide the button
// Do other stuff as needed for this button
}
else {
// Show the button
// Do other stuff as needed for this button
}
}
The same data model should be updated when user selects the button.
Hope this helps.
Problem- 1 You are creating your button every time cellForRowAtIndexPath gets called.
Problem- 2 You have no code to keep the state of your button with cell reusability, that's why it is refreshed when cell is going for reuse.
Solution- For this you have to keep your selected button state. Make a global NSInteger variable. And set its value as sender.tag value in like: method. And use -
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
// here create your UIButton
// Set its tag to indexPath.row
// add this to cell as subview
}
// find button of current cell using tag
UIButton *btn = (UIButton*)[cell.contentView viewWithTag:indexPath.row];
// place your check here if button tag matches with selected tag then disable btn here.
// Else use setEnable with YES parameter.
}
I have a tableview with a few custom cells. Inside the first one is a button. When this button is pressed I want to scroll to a certain Section in my tableview. How do I have to link the button action with the tableview?
You can scroll to a certain section with using this function :
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
the example of usage is :
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:indexPath.section]
atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
and for link the button action with tableview you can use protocol in you custom cell
You can make your cell's button a property and in cellForRowAtIndexPath you can set the target in the class that loads the table view so you won't need any delegates. Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"YourCellIdentifier";
YourCustomCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil) {
cell = [YourCustomCell alloc/init method....
[cell.buttonProperty addTarget:self action:#selector(cellButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
//do other stuff with your cell
}
-(void)cellButtonTapped:(id)sender {
UIButton *button = (UIButton*)sender;
YourCustomCell *cell = (YourCustomCell*)button.superview.superview; //if the button is added to cell contentView or button.superview is added directly to the cell
NSIndexPath *path = [yourTableView indexPathForCell:cell];
[yourTableView scrollToRowAtIndexPath:path
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
Set up a delegation mechanism back from your cell - when creating the cell, assign it an NSIndexPath and pass it back from the cell when the button is tapped.
So in your UITableViewCell subclass you'll have:
- (IBAction)buttonPressed:(id)sender
{
[self.delegate buttonPressedOnCellAtIndexPath:cell.indexPath]
}
Back in the controller that's the delegate, respond to this method with:
- (void)buttonPressedOnCellAtIndexPath:(NSIndexPath)indexPath
{
[self.tableView scrollToRowAtIndexPath:indexPath];
}