Creating IBOutlets and IBActions for UIButtons in a static UITableView - ios

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);
}

Related

How to add more than two labels to prototype cell?

I have gone through the tutorial below and it works fine. My question is how do I add more than the two standard cells to the prototype cell?
http://thedarkdev.blogspot.co.uk/2013/09/web-service-apps-in-ios7-json-with.html
cell.textLabel.text = "Title Text";
cell.detailTextLabel.text = "Detail Text"
I am wanting to add another 4 labels and would like to lay them out using the storyboards.
Any ideas how to do this?
You can use a custom cell type and you'll be able to add as many labels as you want:
Create a empty UITableViewCell subclass that you'll use for this cell. Note, this subclass doesn't need any code inside its #implementation. We're only going to add outlets for its properties, and those will show up in its #interface, but the storyboard eliminates the need to write any code for the cell, itself.
Back in Interface Builder, go to the table view in your storyboard and make sure it has a cell prototype. (If it doesn't drag one from the object library on to the table view.)
Over on the "Identity" inspector panel on the right, set the base class of the cell prototype to be your UITableViewCell subclass as the cell prototype's "base class";
In the storyboard's "Attributes" inspector for the cell, set the cell "Storyboard identifier" to something you'll reference down in step 5 (I've used CustomCell here);
Set the cell "Style" to "Custom" rather than "Basic" or "Detailed":
add your labels to the cell.
I've added for labels to a single prototype cell here:
Use the "Assistant Editor" to show your code simultaneously with the storyboard. Select one of the labels you've added to the scene, change the code down below to be the UITableViewCell subclass you created in step 1, and you can now control-drag from the label to create IBOutlet references for the labels to the cell's custom subclass:
By the way, I'd advise against using IBOutlet names of textLabel or detailTextLabel (not only are they too generic, but it can get confused with the labels that appear in standard cell layouts).
Now your tableview controller can reference this subclass:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell"; // make sure this matches the "Identifier" in the storyboard for that prototype cell
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// retrieve the model data to be shown in this cell
// now fill in the four labels:
cell.firstNameLabel.text = ...;
cell.lastNameLabel.text = ...;
cell.emailLabel.text = ...;
cell.telephoneLabel.text = ...;
return cell;
}
So while there are a couple of steps to go through here, the net result is that you can design whatever cell layout you want, and with this very simple UITableViewCell subclass, your cellForRowAtIndexPath is incredibly simple, just referencing the IBOutlet references you connected in Interface Builder.

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

IBActions for UITableViewCell prototype

So far, have read a few posts, such as this and this, but they have not really helped with my situation.
I'm creating a dynamic form for iPad using 'plain' style UITableViews. I have multiple different UITableViews on the page, so I defined a separate object to server as my datasource and delegate. I understand how to change the text of each cell using the datasource; however, I have no clue how to link the UITextFields in my prototype cells to an IBAction. I could figure out how to create a single IBAction for all textfields in my table, such that they all update the same data, but I don't know how to have each UITextField have a one-to-one correspondence with my datasource.
Here is my prototype cell:
and my code thus far:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myPrototypeCell"];
UILabel *buildingNumber = (UILabel *)[cell viewWithTag:100];
buildingNumber.text = [#"Building " stringByAppendingString:self.dataSource[indexPath.row][#"buildingNumber"]];
return cell;
}
self.dataSource is an NSMutableArray of NSMutableDictionaries.
Any help whatsoever is appreciated.
I initially thought you were referring to IBOutlets so my previous answer is somehow wrong but the inherent idea is still the same.
You cannot have IBActions or IBOutlets from a prototype cell unless the cell is subclassed. You can do so if the cells are static though, not that it can help in your case. Subclassing the UITableViewCell is not too hard or too bad, in fact if in the future you want to speed things up on your TableView, that is one of the many ways to start.
This tutorial provides a few different options for dealing with information inside a table view cell:
http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
I almost always use a UITableViewCell subclass to deal with outlets and actions inside the cell. But this should be a decision you make based on your own architecture.
Hope this helps!
you need single for all your textField.So do the following:
Get the text field as your are getting label
UITextField *yourTextField = (UITextField *)[cell viewWithTag:101];
[yourTextField addTarget:self action:#selector(clickTextField:) forControlEvents:UIControlEventEditingChanged];
clickTextField method will get invoke for every Text Field.
Hope this helps.
Edit: Forgot to mention, you can you set delegate of UItextField and get a notification in UITextFieldTextDidChange: delegate method

Set detailtextlabel of static cell programmatically

I have a tableView with a few sections and I have it set for static cells instead of dynamic prototypes. The problem is that I can't set the detail text label of a static cell programmatically or at least I don't know how. Is it possible ? The only way I see of doing this is having dynamic prototypes which means I'm going to have to deal with setting up all the cell.textLabels in my dataSource and also all the sections and my segues will not work anymore. If anyone has ideas it would be great help. Thanks :)
Assuming that your UITableView is in a UITableViewController, here are 2 approaches that are useful:
Custom UITableViewCell: In the view controller class, declare a property for a label as: #property (strong) IBOutlet UILabel *labelInCell;
In the storyboard, drag a UILabel into the cell, select the controller's Connections Inspector, and connect the outlet of the property by dragging from the inspector to the UILabel object.
You can then assign the label text programmatically, for example, in viewDidLoad: of the controller class.
Standard datasource: Alternatively, you can implement just one method: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath in the view controller's datasource and set the detailTextLabel property there.

Changing the contentView when an Accessory button is tapped

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.

Resources