change content of a tableviewcell with the next cell content - objective-c

i have a situation like this one: example image
I have a first tableviewcontroller, with main categories; a second one with elements for each category; and a viewcontroller which show the content for each selected elements. I'd like to add in my viewcontroller a button-action or a swipe gesture, in order to change the view content to the next element content, without going back to the subcategory tableview and selecting another cell. I already know how to change view with button or gestures, but i'd like to know how to get 'next cell' (and previous one), inside the actual one.
Thanks for any advice

The UITableView is a visual representation of your datasource, let's say an NSArray of NSDictionary objects. So it seems obvious that your UIViewController is a detail representation of the same datasource. So you could pass this array and the indexPath of the selected cell in tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath to your viewcontroller and store it for further use. When you swipe left / right you just increase / decrease the selectedIndex and display your datasource's content.

Related

iOS animate partial cells to show full cell

I've got an UITableViewCell with a UITextField in it. When I click the UITextField I want to make sure the entire cell is visible in the tableView. I did this in textfieldDidBeginEditing, like so:
- (void) textFieldDidBeginEditing:(id)textField {
TTTableViewCell *cell = (TTTableViewCell *)[self.tableView cellForRowAtIndexPath:objc_getAssociatedObject(textField, kIndexPathId)];
[self.tableView setContentOffset:CGPointMake(cell.frame.origin.x, cell.frame.origin.y - self.tableView.sectionHeaderHeight) animated:YES];
}
This works perfectly when the user goes down the list. Expect when to user taps a cell which is partial visible on the top it doesn't reveal the entire cell just moves a bit.
Does someone know a solution to this problem?
There are UITableView methods to solve exactly this. You can use scrollToRowAtIndexPath:atScrollPosition:animated: to ask the table view to bring a cell to the top by passing UITableViewScrollPositionTop.
(You might first want to check if it's already visible to ensure it doesn't scroll unnecessarily.)

Show UITableViewCell which can then expand to show more data

I have a UITableViewCell which is currently cluttered and has too much information on the screen at once. I want to make a button, that when pushed, a small view will slide down and show the remaining information, and then when pushed again it will slide back to the original size. I was wondering how I go about doing something like this. I know NOTHING about doing this, so please be specific when pointing me in a certain direction. Thanks!
Do you want single Button for all cells in table view or you want to add button as subview on each cell
1.if you want to add button as subview on each cell
a) Add button with tag value equals to indexPath.row and set target to single method for each button.
ex
-(void)infoButonTapped:(UIButton *)sender;
Now on Click on button find the tag value of button and get the info from the array which you use to populate UITableViewCells.
b) Now create a infoview and add textview on it and set info as text prop of text view and add infoview as subview of view using UIView Animation and hide on next click
you can either use a bool variable in .h file to know hide or show infoView or you can check if infoview has superview then you have to hide infoview else add infoview as subview of self.view.
if you want a common button for all Cell
a) Now on click of button get indexPath for selected cell of tableview using tableview method
- (NSIndexPath *)indexPathForSelectedRow;
this method either return indexpath for selected cell or nil in case no cell selected
Use indexpath for get info from the array and add infoview as subview to view using step 1.b explained above.
you can also get the rect of selected row for setting the cordinate for the info view
by method of tableview
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
This may help you Show hide table
Or you can use BOOL as a property of cell and hide the subviews of UITableViewCell according to bool also resize frame of cell.
In this case, you better use the following method: -
(void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{
}
Make a call to above method in your UITableView didselectRowAtIndexPath method.
More explanation is given in below link
UITableViewCell expand and collapse

Can I add a disclosure Button to only one cell in my UITableView?

I have a UITableView and I want to add a disclosure button to a cell but only when the cell is selected.
How can i do it?
Thanks
in tableView:didSelectRowAtIndexPath: save the indexPath to a member variable.
in tableview:cellForRowAtIndexPath: check, if the indexPath is the saved one and set cell.accessoryType
Do it in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//bla bla bla
}
Remember to have a variable to remove the button from the previous selected cell
In the table view cell set the accessoryType or accessoryView property. You will want to do this in the -tableView:didSelectRowAtIndexPath: delegate method.
Doing it in didSelect means that the disclosureButton only appears after the user releases the selected cell. If you want ti to appear when the cell is highlighted, you need to subclass UITableViewCell and override setSelected: or setHighlighted:
However, the whole point of a disclosureButton is that the button can be pressed and does something different then simply selecting the cell. For that there is the disclosure indicator. The whole premise of making either of these show up only when the cell is highlighted/selected doesn't seem to serve any good UI design purpose in my opinion...but maybe for what you want it does make sense.

How to make a custom tableView cell accessory

I have not yet found any really good examples on how to do this. There is an image that I want to use as the accessory button and when I put it in and click on it doesn't work. So it looks correct but doesn't work...
Here is my code:
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
cell.accessoryView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"TableView_Green_Disclosure.png"]];
So how do I get my UIImageView to call accessoryButtonTappedForRowWithIndexPath whenever it is tapped?
A thorough reading of accessoryView and accessoryType would reveal that they are mutually exclusive ways to customize a cell.
Setting the accessoryType will cause the table view delegate method to be called when it is tapped.
Setting the accessoryView will ignore the setting of accessoryType and give you something to display. If you want to receive a callback from the custom view you've put in place, it should be a control that is wired up to do so. (Or any view with a gesture recognizer.)
If you use a button, and set its action to accessoryTapped:, you will receive the button as the "sender" argument. You can walk up the view hierarchy until you find a table view cell, and then ask your table view what the indexPath of that cell is. This will then get you an index into your model objects and you be able to act on it appropriately.
Alternate to the button, you can enable interaction on the UIImageView above, and add a gesture recognizer to it.
To make the button actually do something, you'll need to implement - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath from UITableViewDelegate.
When an accessory button is tapped in a row, this method will be called and you'll have the chance to act appropriately using the passed in index path to determine which row's accessory was tapped.
Check the blog post hdr->cmdline for creating custom accessory view for UITableView.
The author used UIButton objects with images for custom accessory view.
To make use of the accessoryView - you would need to set the cell's accessoryType to UITableViewCellAccessoryNone deposit a UIButton (with associated image) into the cell and then wire it up to receive user touches. You might use something like the code below as the IBAction response to the cell's UIButton being touched:
- (IBAction) accessoryButtonPressed:(id) sender
{
NSUInteger pathInts[] = { 0,0 };
pathInts[1] = self.currentselectedrow; // ivar set when tableview row last selected
NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:pathInts length:2];
[self tableView:mytableview accessoryButtonTappedForRowWithIndexPath:indexPath];
}
The UIButton would be wired to execute this glue code by way of a line inside your tableview's "cellForRowAtIndexPath:" function
[thecell setButtonTarget:self action:#selector(accessoryButtonPressed:)];
One thing I noticed is that the UIButton seems to want a 'swipe right' versus a simple 'tap' touch in order to trigger the event - but it could be my beta iOS that's the problem. Note that I had added a UIButton* object named 'cell_accessoryButton' to the Custom Cell source.
In the cell's source you'd support the 'setButtonTarget' call with code like this:
- (void) setButtonTarget:(MyViewController*)inTarget action:(SEL) inAction
{
[self.cell_accessoryButton addTarget: inTarget
action: (SEL) inAction
forControlEvents: UIControlEventTouchUpInside];
}
It's so much easier to just use the accessoryType reference and let iOS do the heavy lifting - but, if you want a custom graphic, etc - this is another path that works.

How to control a view with different UITableViewControllers based on parent table selection

I have a Split-View iPad application and I fixed the selections in the popover (RootViewController) to be static, let's say red, yellow, and blue. When my app starts I have preselected red or the top static text in the cell. I was able to add UITableViewDelegate and UITableViewDataSource with the other templates in DetailViewController. It's the one with the popover-no-popover default split-view app.
I added the row count and the cell method and voila' my test array populated the detail table. I want a separate controller (delegate and source) for each selection the user chooses to be driven from didSelectRowAtIndexPath in the RootViewController.
Should I just add the delegate and datasource templates to the DetailViewController and switch the data based on selections in the RootViewController view?
Or, would my multiple controller pattern be better design?
I would like some assistance as to how to get the outside controller to have control of the DetailViewController's UITableView. The DetailViewController is where I instantiate the add button and such to the toolbar. E.g. when I added the single (Red) test controller to the DetailViewController, any connection to the TableView wasn't seemingly automatic in IB. You could hover and then it would eventually connect to "View" (ultimately the UITableView). I've tried everything and I cannot get a simple delegate and datasource controller with a simple NSArray to populate the DetailViewController's table view from RootViewController's didSelectRowAtIndexPath. This method works because I've debugged and NSLogged the selections. I'm not creating my ProjectViewController (test or first static text in RootViewController) correctly. I'll paste some code that I've tried here too.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// select static row and view controller
Punch *obj = [self.punchList punchAtIndex:indexPath.row];
NSLog(#"Selected punch object: %#", obj.name);
ProjectViewController *projectViewController = [[UITableViewController alloc] init];
[projectViewController tableView:detailViewController.tmpView];
}
The ProjectViewController code works if hard-coded in DetailViewController, i.e. the required methods, the count, and the array loaded cells and the I want this data population in separate controllers because I'm going to use Core-Data in the end.