Adding buttons to a UITableView - ios

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.

Related

IBAction for Buttons inside static Cell do not work with didSelectRowAtIndexPath is overrided

I have some static UITableViewCells. One of them with 5 buttons inside. Inside didSelectRowAtIndexPath I handle the selection event for all the cells except for the one with the buttons inside. The problem is the action for the buttons in that one cell does not work.
Any help will be appreciated.
Finally I figured out That the root of the problem was at a completely different area, heightForRowAtIndexPath was returning wrong height for that cell (actually smaller height),
So, the controller interpreted the touches to be in a completely different place than the actual place I was supposedly touching
You don't have the cellForAtIndexPath method, however you can implement the tableView:willDisplayCell:forRowAtIndexPath: method and add target for each button like so:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// get a reference to button
UIButton *button = (UIButton *)[cell viewWithTag:1];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

Get tableview row on cell button tap

I have a tableview with cells (not custom), and each cell has a button (among other things).
Depending on the cell, the button that is created is different. I have 3 different types of cell/button, Rate/View/Edit (I'm not sure that's at all relevant)
This button is only used for this:
[button addTarget:self action:#selector(rateEvent:) forControlEvents:UIControlEventTouchUpInside];
The selector is different for each button (rateEvent, viewEvent, editEvent).
Here is an example, they're all 3 fairly similar and start the segue sequence:
- (IBAction)viewEvent:(UIButton*)sender
{
[self performSegueWithIdentifier:#"fromHomeToDetails" sender:self];
}
My didSelectedRowAtIndexPath saves the details of a meeting in the selectedMeeting object.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedMeeting = [_nextMeetingsArray objectAtIndex:indexPath.row];
}
The segue sends selectedMeeting to the next controller.
My issue : The meeting that is sent depends on the selected cell. That means the user has to select a cell and THEN tap the button to get the correct details on the next page. What can i do to get the correct indexpath of a tapped button inside a cell?
I tried to add parameters to the viewEvent method, to make it look like -(void)viewEvent:(int)index:(UIButton*)sender; but it doesn't work in the #selector(viewEvent:).
The solutions i can imagine but not create are :
- Manage to get the indexpath of the cell of a tapped button and send it to my viewEvent method, somehow.
- Create a customCell and/or use an accessory, so i've read here and there.
- Force row selection when i tap the button, but that also requires to know the index of said row.
Both seem "too complicated" for something that (should?) be fairly simple. I'm pretty sure any experienced programmer will have an obvious easy answer that I haven't learned yet, and i'd rather ask for it than implement something heavier than it should be.
Also, if you have any comment/criticism on how i've done that, i'm all ears :)
Thanks for your time guys, as always, i'm very grateful.
EDIT :
cellForRow method :
Note : i removed everything that has to do with labeling. Because i have different buttons there are multiple if{} statements but they are of no influence in my opinion. Also, the cell is based on a storyboard cell with a tag of 228 ; after creation a new tag is set so it shouldn't influence. Also, i have a cap of 50 items in tableview (set from webservice) so there is no way the indexpath reaches >50.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *nextCell = [self.tbvNextMeetings dequeueReusableCellWithIdentifier:#"tbNextCell" forIndexPath:indexPath];
UIButton *bt= (UIButton*) [nextCell viewWithTag:228];
[bt setTitle:#"Edit" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(editEvent:) forControlEvents:UIControlEventTouchUpInside];
[bt setTag:indexPath.row];
return nextCell;
Add tag to button as indexpath.row, in cellForRowAtIndexPath() method. Then in selector method get sender tag. And pass in
NSIndexPath *path = [NSIndexPath indexPathForRow:sender.tag inSection:yoursection];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
Edit : Setting the cell with a tag of 228 at first and then switching it to the value of indexpath made its creation fail.

Creating a button for every UITableViewCell

I'm trying to create an friend request function. Where all the friend request would show up in a table and a player will get to click accept or decline. What I'm trying to do is create an accept button beside this UITableView that contains all the player's friend requests.
Here's my code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *notificationCell = [tableView dequeuREusableCellWithIdentifier#"notificationCell" for IndexPath:indexPath];
NSArray *friendRequests = [self fetchAllFriendRequestsInArray];
NSManagedObject *friendRequestingRelationship = [friendRequests objectAtIndex:indexPath.row];
notificationCell.textLabel.text = [friendRequestingRelationship valueForKey:#"name"];
UIButton *acceptButton = [UiButton buttonWithType:UIButtonTypeSystem];
[acceptButton.frame = CGRectMake(notificationCell.frame.origin.x + 150, notificationcell.frame.origin.y -20, 80, 40);
[acceptButton setTitle:#"Accept" forState:UIControlStateNormal];
acceptButton.backgroundColor = [UIColor clearColor];
[acceptButton addTarget:self action:#selector(acceptButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[notificationCell.contentView addSubview:acceptButton];
return notificationCell;
}
Only the first notificationCell showed the friendrequester's name and Accept Button. Other notificationCells only showed other friendrequesters' names without the button. May I know what is wrong with my code such that I can allow the button to be shown on every single cell?
Thank you in advance!
The buttons are there, but they are clipped from the view. This line is the culprit:
acceptButton.frame = CGRectMake(notificationCell.frame.origin.x + 150, notificationcell.frame.origin.y -20, 80, 40);
You shouldn't add the origin of notificationCell to the button, because subview positions are relative to positions of their superviews.
This should give you the right look, but your code has other potential problems.
The line where you fetch all friend requests is probably too slow to be executed for each cell in the view. Make sure that the results are cached
Table view cells are recycled. When one of such recycled cells makes it to your code, it looks like your code adds a second button on top of the first one
Similarly, if a recycled cell with a button is returned for the cell that does not need a button, the old button would remain visible.
You may be better off using a prototype cell that already has a button on it. Instead of adding and removing that button, you could make the existing one visible or invisible, depending on the context.
This is a bad approach to solving this problem to begin with. You should go to the storyboard, and drop a button in a prototype cell. Then select that button, go to the attributes inspector, and set the "tag" to a number of your choice (for this example we will say 1). You can then get the button for each cell like so after grabbing the cell:
UIButton * acceptButton = (UIButton *)[cell viewWithTag: 1];
[acceptButton addTarget:self action:#selector(acceptButtonPressed) forControlEvents:UIControlEventTouchUpInside];
This is cleaner and will give you the results you want.

Add multiple buttons to IOS7 Tableview

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

How to add a Round rect button outside of a cell in a UITableView?

How do I add a round rect button outside of a cell in a UITableView?
For example :
The "Other","home" & "home" fields are all independent cells.
What I am trying to achieve is the ability to add buttons such as the "Text Message","Share Contact" & "Add to favorites" shown above.
Thanks in advance for the advice!
Simply add a footer (or header) to the appropriate section.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *yourView = [[UIView alloc] initWithFrame:CGRectMake(x,x,x,x)];
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
yourButton.frame = CGRectMake(x,x,x,x);
[yourButton setTitle:#"Title" forState:UIControlStateNormal];
[yourView addSubview:yourButton];
return yourView;
}
Basically there are two options within regular UITableViews.
It is a cell with transparent background of the content view and of the cell itself.
It is a section header. Noting stops you from inventing a new section exactly there, a section with 0 rows even. And you overwrite the tableView:viewForHeaderInSection: method and provide an appropriate header with buttond and whatever you like. This one, too, shold have a transparent background.
You need a custom cell with a transparent background and you can add those buttons on it. Alternatively, you could use the section footer to do pretty much the same.

Resources