Change a button dynamically in a table view cell - iphone

I have a table where 5 cells are there. In each cell there is a button. I need to change those buttons dynamically on some conditions. How to change the button dynamically in table view.

What you can do is Write a function which will change the values of the buttons and then you can call that function. After changing button values then use any one of the mentioned method below:
You can use either : [tableview reloadData]; to reload all the table data.
OR
You can reload particular rows using the following method :
[tableview reloadRowsAtIndexPaths: indexPath withRowAnimation:anyKindOfAnimation];

You can query your table view for all visible cells through -visibleCells, get the reference of your button (assuming you have a UITableViewCell subclass with a property for the button) and change them.

First you need to set tags for each buttons. This can be done by
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Your code
cell.Button.tag = indexPath.row;
[cell.Button addTarget:self action:#selector(buttonClick1:) forControlEvents:UIControlEventTouchUpInside];
}
Then you can get the button from this method
- (IBAction)buttonClick1:(id)sender {
int tagNo =[sender tag];
UITableViewCell *cellclicked = [self.tblProfileFollowing cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tagNo inSection:0]];
//Now change the action for the cell
[cellclicked.Button addTarget:self action:#selector(buttonClick2:) forControlEvents:UIControlEventTouchUpInside];
//Your Code
}
You can follow the same steps for - (IBAction)buttonClick2:(id)sender
Now you can track which button was clicked and change the method for that button.
And declare your cell and button strong.

Related

Get row number of UITableView in IOS [duplicate]

This question already has answers here:
get section number and row number on custom cells button click?
(5 answers)
Closed 9 years ago.
In my code I have a table view with many sections sorted alphabetically. I have checkboxes(UIButton) in table rows and I need check them according to respective status entered in row. I had done
checkbutton.tag = indexpath.row;
but when table scrolls and section get changed and I get status of previous section in my checkbox method. anyone please help me on this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
[CheckButton addTarget:self action:#selector(CheckBoxAction:) forControlEvents:UIControlEventTouchUpInside];
CheckButton.tag=indexPath.row;
//some other stuff
[cell.contentView addSubview:CheckButton];
}
and this my CheckBoxAction
-(void)CheckBoxAction:(UIButton *)btn
{
tag = btn.tag;
NSDictionary *tagdict =[statusArray objectAtIndex:tag];
}
Now, problem is that indexpath.row is 0 whenever section is changed, so I can't able to access exact row number in my CheckBoxAction.
use this for getting current row you tap.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CheckButton = [[UIButton alloc] init];
[CheckButton addTarget:self action:#selector(CheckBoxAction:) forControlEvents:UIControlEventTouchUpInside];
CheckButton.tag=indexPath.row;
[cell addSubview:CheckButton];
}
-(void)CheckBoxAction:(id)sender
{
// Cast Sender to UIButton
UIButton *button = (UIButton *)sender;
// Find Point in Superview
CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];
// Infer Index Path
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInSuperview];
// Log to Console
NSLog(#"selected Row : %d", indexPath.row);
}
// Try this
CheckButton.tag = [[NSString stringWithFormat:#"%d%d",indexPath.section,indexPath.row] integerValue];
or
// try this below link to get logic for, how to handle checkbox in UITableView
Set checkmark in UITableView
What you are doing is wrong in many ways. So, I am not going to give you a solution for the exact problem you have, but more of a guideline:
First you are adding the checkbox directly to the cell, which means you are possible adding multiple CheckButton to the same cell, if you scroll your UITableView a few times.
Add the checkButton on the cell directly instead of keep adding it in the cellForRowAtIndexPath, this can give you a performance boost.
Use a block that will be executed when a user clicks the button. This way you are able to capture the context of your UIViewController. You should assign that block to the cell on cellForRowAtIndexPath

Hide custom cell image

I am displaying a button in cutstom cell in UITableview. How do I hide that button when it is not needed. For example: I am display received images count on button. In case count will be zero, I need to hide that button from cell.
contactviewController.m
if (![[arr objectAtIndex:4] isEqualToString:#"0"]) {
[cell1 setImg:[arr objectAtIndex:4]];
}
Customcell.m
-(void)setImg:(NSString *)_text
{
imgView.titleLabel.textColor = [UIColor whiteColor];
[imgView setTitle:_text forState:UIControlStateNormal];
}
You can do this in your cellForRowAtIndex method. For each row creation this method is called.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(condition)
{
//show button
}
else
{
// don't show button
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(noImagesFound)
{
yourCustomCellObject.buttonObject.hidden = YES;
}
else
{
yourCustomCellObject.buttonObject.hidden = NO;
}
}
You can add buttons in cell via xib as IBOutlets or programmatically by setting frame and adding the button to the cell. The solution for above depends on how you have added the button. If you added it via xib as an outlet, you can either do one of the following.
There a property to any view called tag. You can set the tag value in the attribute inspector for the button and in the -cellForRowAtIndexPath: call as
UIButton *button =(UIButton*) [cell viewWithTag:9];
// your tag value (say 9)
and the use [button setHidden:YES] to hide //NO to unhide here you dont need a custom class
If you have created custom class for the same ,where you can add the button via two methods either by programmatically or via xib. If its xib, create a IBOutlet and make it a property of the custom cell so that you can get access to it using the custom cell object.
It's better to opt the second option if you need more control over the cell you created. If you had made it a property you can get access to it by using the object of the cell.
Create UITableViewCell.h and .m file.
Create some variable like UILabel and UIImageView object in files and make it IBOutlet and bind them with cell .xib file.
Inside UITableView implementation, and in "cellForRowAtIndexPath" you can use that custom UITableViewCell class object and use that synthesize variable of UILable and UIImageView and use it to show or hide accordingly.

iOS: UITableView clarifications

How can I eliminate views created upon selecting a cell in UITableView when the cell is no longer selected or other cell is on selection.
Suppose I have the code below for didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size;
UIView *selectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (int)cellSize.width, (int)(cellSize.height + 100))];
selectionView.layer.borderWidth = 2;
[[tableView cellForRowAtIndexPath:indexPath]addSubview:selectionView];
}
Now, I want to remove that created selectionView when I focus on another cell and create it again to the cell which I am focusing.. The problem is that when I select a cell for the first time, it works perfectly but when I select another cell, the selectionView created from the previous cell still does not disappear and it duplicates the view already. How am I suppose to solve this? Need suggestion.. :( thanks..
You need to add tag for the selectionView as follows
selectionView.tag = 100;
Also , you need to have the reference of the last selected indexPath by declaring a NSIndexPath class member and retaining it.
So while selecting a new cell, get the cell with last selected indexpath and remove the view from the cell as follows
UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastSelIndexPath];
UIView *view = [lastCell viewWithTag:100];
[view removeFromSuperview];
You really need to look into subclassing UITableViewCell if you want a custom selection UI. Then override [UITableViewCell setSelected:animated:] (or [UITableViewCell setHighlighted:animated:]) to perform your customisations.

UITableViewCell with a UIButton

I have UITable which contains some UIButtons. I would like a way to use the label.
The problem I have is that I need two tag labels, one for retrieving the cell in tableviewCellWithReuseIdentifier and configureCell.
Until recently I have been using the UIButton.title to determine which row in the table I selected. The (invisible) text was the row number.
Now I need to use the title for some visible text. How can I still find which row was pressed?
When I press the area outside the button the usual didSelectRowAtIndexPath is called. The hard part is that I want to capture both the UIButton (which calls its own selector) and the row, which I had been finding out from the title. Now I need the title for something else.
I use something like this. This will get you the indexpath in the tableview.
- (IBAction)buttonAction:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint buttonOriginInTableView = [button convertPoint:CGPointZero toView:tableView];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonOriginInTableView];
// do something
}
You could subclass UITableViewCell or UIButton and add some properties.
You should implement the UITableViewDelegate protocol.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Do something based on selected row (indexPath.row)
}
Implement the UITableViewDelegate protocol. It will help you and do what you need.
didSelectRowAtIndexPath is autmatically called whenever a row is selected in your table.
in your .m file the one with the table view controller if there isn't one there add this method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// NSInteger row = [indexPath row];
// or (indexPath.row)
// do whatever you like with this value which tells you which row is selected.
}
PK

iPhone app development : How can i get a table view in which whn i click on a cell a text view should expand just below that cell...?

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.