How to create custom uiviewtable? - iphone

I'm working on an app for my client. I'm stuck now.
I don't know how to explain. But i make a photoshop image of it.
http://i.stack.imgur.com/cV4mL.jpg
user tap on parent tablecell.
execute didSelectRowAtIndexPath:(NSIndexPath *)indexPath. User select cell.
parent cell update.
Anyone know what is this called?
Do you have tutorial for it?

// FirstViewController (1st in your Photoshop-design)
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.cell = [tableView cellForRowAtIndexPath:indexPath];
[self.navigationController pushViewController:secondViewController animated:YES];
}
...
-------------------------
// SecondViewController.h
#interface SecondViewController : UITableViewController {
UITableViewCell *cell;
}
#property (nonatomic, retain) UITableViewCell *cell;
-------------------------
// SecondViewController.m
...
#synthesize cell;
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.cell.detailTextLabel.text = #"Something";
[self.navigationController popViewControllerAnimated:YES];
}
...

Use UINavigationController with UITableViewController as its root controller and when diving deeper, instantiate a different UITableViewController for that and push it on the navigation stack. Popping is similar.
There are good examples for UINavigationController accessible from Apple's docs.

Related

Separating UITableView's delegate/datasource from main ViewController

I'm quite new to iOS development. I have read about the issues with ViewControllers becoming huge and would like to follow the design I have seen in the answer to this previous question UITableView issue when using separate delegate/dataSource for my app which has 2 different tables and couple of buttons on one screen. But somehow I get confused in the storyboard connections to make between TestTableViewController and TestTableTestViewController.
Can anyone provide a sample working project or some screen shots on how to connect the UITableView delegate, data source and connecting outlet to the separate custom UIViewController subclass (TestTableTestViewController) in storyboard please?
Also, does this design work with xCode 5 / iOS 7 and above?
Note: For those having moved to Swift I strongly recommend using swift extensions for the delegate & data source and in fact any other implementation of an inherited class or protocol as per the 'Grouping' section of Natasha The Robot's blog post here
You have to create another class named as MyTableViewDataSource and implement TabbleViewDataSource methods in it. Create an a property of your data source class. Set data source in ViewController
Here is the examle:
#interface MyTableViewDataSource ()
#property (nonatomic, assign) int sectionOneRows;
#property (weak) id<YourDelegate> delegate;
#property (nonatomic, strong) NSArray *dataSourceArray;
#end
#implementation MyTableViewDataSource
#synthesize delegate=_delegate;
-(id) initDataSourceWithdArray:(NSArray *)array
delegate:(id)delegate
{
if (self = [super init]) {
self.dataSourceArray=array;
self.delegate = delegate;
}
return self;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cellToReturn = nil;
//Your code here
return cellToReturn
}
#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//Your logic here
//implement the delegate method in you ViewController
[self.delegate performSelector:#selector(loadDetailsAtIndexPath:)withObject:indexPath];
}
}
In your ViewController:
self.myDataSource = [[MyTableViewDataSource alloc] initDataSourceWithdArray:self.dataArray delegate:self];
[self.myTableView setDataSource:self.myDataSource];
[self.myTableView setDelegate:self.myDataSource];
[self.myTableView reloadData];
Your delegate method:
-(void)loadDetailsAtIndexPath:(NSIndexPath *)indexPath
{
MyDetailViewController *myDetailController = [[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
//Your logic here
[self.navigationController pushViewController:myDetailController animated:YES];
}
Go to the Connections Inspectors and make changes like this:
Data source for the tableView will be set programatically here rather in IB.
I hope this will help you.
Press and hold Ctrl and click + hold + drag on item you want to make outlet to .h file. it will make connection by itself you just have to name them.
See This Video Tutorial
Also check these tutorials
Link 1
Link 2

sending data from DetailViewController to MasterViewController

This is what exactly I'm trying to do.
wizardviewcontroller.m
- (IBAction)onCountryClick:(id)sender {
MJDetailViewController *detailViewController = [[MJDetailViewController alloc] initWithNibName:#"MJDetailViewController" bundle:nil];
[self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationSlideLeftRight];
}
User click country button a popup shows with list.
when user select a row button title should change.
This is my detailview,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
WizardViewController *mj = [[WizardViewController alloc] initWithNibName:#"WizardViewController" bundle:nil];
mj.countryselected = [countryNames objectAtIndex:indexPath.row];
[mj.countryButton setTitle:mj.countryselected forState:UIControlStateNormal];
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}
DetailViewController is dismissing, but countryButtonTitle is not updating. I know this is because the wizardview is not refreshing. I would like to know the correct workaround in this case.
Hope this helps to get better answer.
Make Protocol in MJDetailViewController
#protocol MJDetailViewControllerDelegate;
#interface MJDetailViewController : UIViewController
#property (nonatomic,assign) id< MJDetailViewControllerDelegate> delegate;
#end
#protocol MJDetailViewControllerDelegate <NSObject>
- (void)selectedContry:(NSString *)title;
#end
And call like
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
NSString *title = [countryNames objectAtIndex:indexPath.row];
if ([self.delegate respondsToSelector:#selector(selectedContry:)]) {
[self.delegate selectedContry:title];
}
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}
Add MJDetailViewControllerDelegate as a protocol in WizardViewController.h)
Now implement selectedContry: method in WizardViewController.m like:
- (void)selectedContry:(NSString *)title
{
[self.countryButton setTitle:title forState:UIControlStateNormal];
}
Hope it helps you.

UITabBarController with UITableView

i am trying to display a list on my first view so added this in the first.h :
#import <UIKit/UIKit.h>
#interface argospineFirstViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *Journals;
IBOutlet UITableView *myTableView;
}
#property (nonatomic,retain) NSMutableArray *Journals;
#property (nonatomic, retain) UITableView *myTableView;
#end
and then i added this on my first.m :
#implementation argospineFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Journals=[NSMutableArray arrayWithObjects:#"journal1",#"journal2",nil];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
cell.text=[Journals objectAtIndex:indexPath.row];
return cell;
}
I am newbie so i don't really know what kind of connections i have got to make, i am also using a storyboard with a tableview dropped on the first view.
is there something i have to add to the delegate?
Any help?
Thank you for your time
Right click on the tableView in the storyboard. You will see "delegate" and "dataSource" under Outlets. Drag the bubble on the right of those to the view controller icon at the bottom of the view. This will make your viewcontroller the delegate and datasource for the table view if you don't want to do it programmatically.
Do not make property of your table view object.
Also,
in viewDidLoad method write:
myTableView.dataSource = self;
myTableView.delegate = self;
Tell me if it helps!
Use initWithStyle instead of initWithFrame for creating your cell.
In your storyboard, select your table view and open the Connections Inspector. Make sure that the delegate and datasource connections are linked to your argospineFirstViewController object.
in IBOutlet set delegete and datasource of the tableview to filesOwner
you use cell.text for show the data i think its not work in tableview just try this line:-
cell.textlabel.text=[yourArrayname objectatindex:index.row];
no need to connect delegate you already define in protocol.

App with TabBar navigation control and TableView method didselectrowatindexpath

I'm newbie in iOS development, and have a problem with my application. I have implemented a tabbar navigation and a TableView, but when call method didselectrowatindexpath i dont see the detailView linked. This is the code of my app:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"ciao");
// UITableViewStyleGrouped table view style will cause the table have a textured background
// and each section will be separated from the other ones.
controller = [[DetailViewController alloc] init];
//initWithStyle:UITableViewStyleGrouped
//andDvdData:[dao libraryItemAtIndex:indexPath.row]];
controller.title = [[dao libraryItemAtIndex:indexPath.row] valueForKey:#"content"];
[self.navigationController pushViewController:controller animated:YES];
}
#import "DetailViewController.h"
#interface ListItemsTableView : UIViewController {
IBOutlet UITableView *myTableView;
Dfetch *dao;
DetailViewController *controller;
}
You're not using the right initializer for UIViewController.
The one you're expected to use is [UIViewController initWithNibName: bundle:] (documentation at Apple linked for you).
I suspect your "controller" object is NULL.

Loading problem of NIB file from TabViewController in iPhone

I have a UITableViewController (MyViewController.xib). This is showing 3 rows with their title. I have 3 new xib file for each row title.On each row selection I want to load XIB file. I am getting the place when I am clicking on RowIndex Selection. But when i am trying to load NIB file nothing is happening. I mean nither program is being crashed nor NIB file is being load.
I am defining my interface declaration here.
#import <UIKit/UIKit.h>
#import "HistoryShow.h"
#interface MyViewController : UITableViewController {
NSArray *tableList;
IBOutlet HistoryShow *historyController;
}
#property (nonatomic,retain) NSArray *tableList;
#property (nonatomic,retain) IBOutlet HistoryShow *historyController;
#end
My implementation details are below.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = [NSString stringWithFormat:#"%#",[tableList objectAtIndex:indexPath.row]]; //typecasting
if([#"History" isEqual:str])
{
NSLog(#"!!!!!!!!!!!!!!!");
HistoryShow *detailViewController = [[[HistoryShow alloc]initWithNibName:#"HistoryShow" bundle:nil]autorelease];
[historyController release];
}
}
This is prining "!!!!!!" on console but next "HistoryShow.xib" is not being load.
What is the exact problem ?
Thanks in advance.
You have to add the view to your present view using addSubview: or push the viewController using a navigationController to see the view.
Something like this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = [NSString stringWithFormat:#"%#",[tableList objectAtIndex:indexPath.row]]; //typecasting
if([#"History" isEqual:str])
{
NSLog(#"!!!!!!!!!!!!!!!");
HistoryShow *detailViewController = [[HistoryShow alloc]initWithNibName:#"HistoryShow" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES]; // if you have a navigation controller
[detailViewController release];
}
}
You are instantiating detailViewController, but you aren't doing anything with it.
Try adding this after the alloc of detailViewController:
[self presentModalViewController:detailViewController animated:YES];