How would I go about adding several buttons to a tableview to each cell? I need to add a comment and like button to each cell in my tableview and each button will have to be specfic to the row being clicked. How do I go about doing something like this ? Do i place a action inside of
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
or is there a way to programatically set a button to a ibaction from inside cellForRowAtIndexPath that allows me to send parameters?
You can create custom table view cell and add multiple button.
1. Create class that subclasses UITableviewcell with xib
2. In xib delete the view and drag a tableview cell into xib
3. Add multiple buttons in xib
4. Create IBOutlet for each button.
5. In your view controller import your CustomTableViewCell and in CellForRowAtIndexPath method add action for each method and set button tag as indexpath.row
6. Identify the clicked button's indexpath from it's tag
Refer this link
I think this is what you are asking...
[cell.button1 addTarget:self action:#selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside]; In the
receiver method you can get sender's tag
-(void)button1Pressed:(id)sender{
UIButton *button1 = (UIButton*)sender;
int selectedRow = sender.tag;
}
You need to subclass your UITableViewCell so you can customise it to your need.
There are a lot of tutorials about this. Depends if you are using Storyboard or not. Here is one of many: http://zeroheroblog.com/ios/how-to-create-simple-tableview-with-custom-cells
Related
I have a UITableViewController, designed entirely with Interface Builder. The UITableViewController is called DonationTableView. I am using Static cells and the UITableView has 6 sections. The reason I am using Static cells is because the content can be populated right from within the Interface Builder and because each cell contains:
1) A different size
2) A label with it's own text
3) A button
I can also use AutoLayout easily within Interface Builder to make sure this DonationTableViewController looks appropriate on all devices. I know I could use code, but I'm still a newbie here and I'm confident with Interface Builder.
I have a custom UITableViewCell class called DonationTableViewCell and I have assigned that to the UITableViewCell in Interface Builder. I am now trying to create an IBAction and IBOutlet for the UIButton in each cell, but with Assistant Editor up, it won't let me actually drag to create that IBAction in the way you usually do. If I change the UITableView to Dynamic, it then allows me to do that, but as mentioned above, I have a fully working UITableView with Static Cells and I just want to create a delegate method in the custom UITableViewCell class so that I can click the button and run an action.
So essentially, I want to be able to assign an IBAction to the UIButton in the UITableViewCell. How would I go about doing that?
Any thoughts would be appreciated.
Drag the button outlet or IBAction to your TableviewController
For use UiButtonin Tableview you need to follow this 2 Steps.
(1) Give UIButton Tag Inside Tableview.
Ex : Here your Button is identify with uniq tag .And also Give UIButton Method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell.button.tag = indexPath.row;
[cell.button addTarget:self action:#selector(btn_Clicked_Method:)forControlEvents:UIControlEventTouchUpInside];
}
(2) In UIButton Method
- (IBAction)btn_Clicked_Method:(id)sender {
NSInteger tag= ((UIButton *)sender).tag
NSLog(#"Button row %ld",(long)tag);
}
I used this code in cellForRowAtIndexPath method. When I click on button or scroll table this button is shown two times. Why is this button shown 2 times please help me?
UIButton *trashbtn=[[UIButton alloc]initWithFrame:CGRectMake(cell.frame.size.width-20, cell.frame.size.height-30, 20, 20)];
[trashbtn setImage:[UIImage imageNamed:#"editor_trash"] forState:UIControlStateNormal];
[trashbtn addTarget:self action:#selector(DeleteMyAssociate:) forControlEvents:UIControlEventTouchUpInside];
[trashbtn setTag:indexPath.row];
[cell addSubview:trashbtn];
You should firstly add a UITableViewCell within your table in IB. Then give an identifier e.g. "MyCellIdentifier" to that cell. Still on IB, add your outlets, to that cell e.g. the button, the textFields... You can initially set the button to be invisible. Then in the method cellForRowAtIndexPath, you do:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCellIdentifier"];
if(cell){
myButton.hidden = NO; //
myTextField.text = #"BlaBlaBla";
}
}
Hey I can give you a suggestion that use auto layout and storyboard to add button rather than adding it programmatically. That is more better and cleaner approach.
The problem you are facing is that cells are reused in tableviews, So the first time you create a button and add it to the cell, it appears once. But if it is dequeued, it already has the button, so when you add another one you end up with multiple buttons.
There are two ways to correct this.
Firstly, remove the button in the cell's prepareForReuse method, which is called just after the cell is reused.
Secondly, avoid creating custom views in the cellForTableView... method. Use a custom cell that already has the button. Now, you may ask, how do I hook up the action for the button if I do it this way? You can either provide a delegate method for your cell that calls back to your view controller, or you can pass your cell a block to perform when the button is clicked.
as far as i can get u already have a custom cell.. add a button to existing cell and create an IbOutlet for the same in customCell.h file
then in cellForRowAtIndexPath method access the button using its IBOutlet
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
customCell *cell =(customCell*) [tableView dequeueReusableCellWithIdentifier:#"customCell" forIndexPath:indexPath];
[cell.btn setTitle:#"blabla" forState:UIControlStateNormal];
[cell.btn setTag:indexPath.row];
[cell.btn addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
if(condition)
{
cell.btn.hidden=YES;
}
else
{
cell.btn.hidden = NO;
}
return cell;
}
Do not forget to register for the customCell class in your ViewController class
[tableName registerNib:[UINib nibWithNibName:#"customCell" bundle:nil] forCellReuseIdentifier:#"customCell"];
you can hide or unhide the button as per your requirements as well as add different actions to the button in each class.
Happy Coding..!!
Do vote it if my code was helpful for you.. ;)
Good day! I have an app that has UITableView with Custom Cell in it. What i want to do is when i click on a specific row it will do an action based on what is the text on that row or cell if possible. For example. lets say this is a tableview.
Flowers 12 Red
Meat 30 Dry
Mouse 10 Alive
Pen 12 Black
Then i clicked on the Meat row and i have a blank text box, it will show the text "Meat" in the Textbox or label. is that possible? thanks!
Could do something like this:
Remember to have a datasource and a delegate of UITableView and declare your UITextField as a mainTextField property.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
mainTextField.text = cell.textLabel.text;
}
If you need a more complex structure, you can create UIButtons for each row and with a UITableViewCell subclass with a custom delegate you can catch the events within a custom cell.
add tags to subviews and put tapgesture for cell, and then in tapgesture target u can acces those views based on tags by subclassing uitableviewcell based on tapgesture tag...
I have setup a tableview that currently has one section, header, and two cells. For username and password, image shown below.
I would like to add a button (IBOutlet), and a label/link below this button within the Table View. What is the correct way to approach this programatically as I am not using interface builder at the moment.
Should the button be a new cell and section? Should the button be a header view and if so how do I control the height on only this header view?
you can try this inside your cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
where index path.row == 3
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(cell.contentView.frame.size.width/2,10,100,20);
[btn setTitle:#"Login" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
sorry for naming convention
I would suggest just adding another row to your table. If you add a second section with only one row the grouped tableview style will make it look like a simple button. If you need more complex styling you can of course create a custom UITableViewCell with one or more UIImageView(s).
Regarding the actually touch event I would then just stick to the tableView:didSelectRowAtIndexPath: delegate method.
If you have only one touch event for the whole UITableViewCell there isn't really a need to add a UIButton.
I would suggest to use simple text fields instead of a table view. However, if you want the table view approach, you should use another section, and make that button a table cell.
I've subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view controller for the table view, in the method cellForRowAtIndexPath: I've enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton.
Cells display correctly.
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. My approach was the following:
In the subclassed UITableViewCell, inside layoutSubviews, create the
CGRects for the "alternate" labels, position them in the same
places as the two "original" label and hide them via setAlpha:;
When the disclosure button is tapped swap out the set of two
label by adjusting their respective alphas.
The problem is I can't figure out what logic in layoutSubviews I'd use to know whether the accessory button has been tapped. I see that in the view controller accessoryButtonTappedForRowWithIndexPath: is called when that button is tapped and from there it seems like I would call layoutSubviews but can't figure out how to use that fact to accomplish what I'm trying to do.
Am I going about this all wrong? Instead of hiding/showing CGRects with alpha should I simply be creating another subclass of UITableViewCell?
When I tap on the accessory disclosure button I want to replace the two UILabel subviews with two different UILabel subviews.
I'll do the following. Create a public method in your UITableViewCell subclass like the following:
- (void)changeContentForCell;
In this method you could set the contentView as you prefer.
Then in your view controller implement
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cCell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cCell changeContentForCell];
}
This is a simple example for change the content. In my opinion you don't have to use layoutSubviews to add views.
Leave this logic in changeContentForCell and then call setNeedsLayout to change your layout. Maybe you could have a variable that tracks the state for your cell: normal state or modified state.
Hope it helps.