Assign different buttons, with different behaviours for different columns - ios

In my project I've got UITableView.
I need to assign different buttons, with different behaviours for different columns.
Question:
Where is the correct way to set different behaviours & actions for buttons? Is it in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath?
How is the correct way to assign different buttons with different behaviours to different columns? I am thinking about [_buttonArray objectAtIndex:indexPath.section]; is it the right way?
Example: Column 1 - Button A -> after button A pressed change button.title and size and do X.

You should use the below method for button behaviors if you want the user to be able to hit the entire cell and have the button action fire. If not, you should use an action delegate in your custom cell class. Let me know if you need help setting up the protocols file and or delegate method.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

Related

How to obtain the view showing and reload the menu table while using SWRevealViewController

I am using https://github.com/John-Lluch/SWRevealViewController to develop my apps. I have two questions while I building my apps.
In the SidebarViewController.m, which is came from the tutorial here http://www.appcoda.com/ios-programming-sidebar-navigation-menu/,
I edited the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
where I want to disable specific menu button on specific view by setting cell.userInteractionEnabled property, for example when I'm in the vc1, I disable the segue that will persent vc1. By this mean, I want to find out the current viewcontroller's restorationIdentifier in order to decide the selection, but I can't find any method to get the current viewController showing, I've try
self.revealViewController.frontViewController.presentedViewController.restorationIdentifier self.revealViewController.frontViewController.childViewControllers objectAtIndex:
which is all wrong.
2.Besides the segue function to change view, my apps support manually change view, suppose I press some button on vc1, the apps will push vc2. By this mean, the SidebarViewController.m method,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
wont call again, so that the setting previously wont apply to the new vc2. I tried [tableview reload] , but still dont work.
Thanks for everyone's great help.
I don't know if this will help you but I use this code to localise my sliding menu if language settings are changed (my localisation is inside the App):
Inside SWRevealViewController.m.
- (void (^)(void))_rearViewDeploymentForNewFrontViewPosition:(FrontViewPosition)newPosition
...
if ([_rearViewController isKindOfClass:([tvcMenu class])])
{
[((tvcMenu*)_rearViewController).tvMenu reloadData];
}
return completion;

customising cell editing style in uitableview in ios

I want to give two options when user swipe right direction.
The two option or button with name edit and delete.
At present it is show only delete when user swipe left i.e UITableViewCellEditingStyleDelete
But i want to customize this view and give two button with gray background.At present it is red background.
The code is :-
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
Please help me how to acheive this.
You need to subclass UITableViewCell and override
setEditing:animated: and
layoutSubviews
This should work in the expected way.
The button is not customisable, if you want to do that behaviour you'll have to create your own custom cells and add the animations yourself.

Changing UITableView functionality on button press

I'm trying to figure out the best way to do this - I have a UITableView of items which the user has previously selected and which is stored. When you click an item it takes you to a detail page. What I need is to be able to click a button below the table view which reloads the table and changes the accessory so its a tick instead of a disclosure, then the user can un-tick the items and remove them from the list, before clicking another button which reloads the updated table and restores the disclosure accessory.
Question is, what is the best way to "remember" which way to handle the table reload after the click so it knows which way to display it? Would you use the NSUserDefaults to store a flag on the button click or is there a more elegant way to do it? I guess I could use the status of the button, whether it's in one state or another, but I'm guessing there is something in-built I'm missing.
Hope that makes sense - thanks.
Usually I have some 'model object' and depending on array of those objects I construct table. My cellForRowAtIndexPath looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
InterestsCell *cell = (InterestsCell*)[tableView dequeueReusableCellWithIdentifier:kInterestsCellID];
if (cell == nil) {
...
}
Interest *i = (Interest*)[self.interestsArray objectAtIndex:indexPath.row];
cell.myTfSubview.enabled = i.isChecked;
return cell;
}
If you don't have your model then you can create array of BOOL values and store flags there.

UIButton grouped Buttons

I'm trying to create a button set up exactly like this:
How would I do this? Can someone show me the code? Is there a way to "Group" UIButtons like this?
You'll want to use a UITableViewController with the sections set to be UITableViewStyleGrouped.
Each of the groups is a section, so you'll want to return how many sections you have with - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView.
For every section, you want to specify how many rows there are with - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section.
You'll want to customize each cell with - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath (which, by the way, tells you which section's particular row you're modifying in the indexPath variable).
Finally, you'll want to handle the row click in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, using the pattern XCode provides for you:
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
Hope this helps!
That's a one-group UITableView with two rows (cells). The table style is UITableViewStyleGrouped. I'd recommend responding to selection of the cell using the delegate's –tableView:didSelectRowAtIndexPath: method as opposed to using UIButtons within the cell -- much nicer visual effect.
You can't group uibuttons like that. You can achieve this look-and-feel several ways, the most elegant is to use the grouped style uitableview.
However if you would want your buttons only to look like this, but not exactly like this, then you could also specify images for you buttons. Uglier way to do it, however much simpler ...

Inability to delete table cells after a certain point

I have built an app which has the ability to delete cells from a table, but only if there are more than two cells.
If there are two or less cells, it only lets me select them. Any ideas?
Here's a video to visualize it: http://slavingia.com/etc/helpme.mov
If you have the datasource, in the delegate method
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
you can see how many items are in the source, if its less than 2 then you can return none in this method which wont allow editing, otherwise you can return delete style so you can

Resources