Custom UITableViewDelefate doesnt work at all - ios5

So, Im following this great tutorial at http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2 to get started with iOS programming.
And I decided that I would like to have custom made headers for my table, and as always I found lots of info here on stackoverflow, that I should implement the method viewForHeaderInSection in my UITableViewDelegate.
So, since I'm using storyboard I thought I'd create my own class of UITableView and then use it for my table in the storyboard.
I have also selected "MyTableView" as "Class" under "Custom Class" in the"Identity inspector" for my table.
My subclass of UITableView (MyTableView.h) looks like this:
#import <UIKit/Uikit.h>
#interface MyTableView : UITableView <UITableViewDelegate>
#end
And MyTableView.m looks like:
#import "MyTableView.h"
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
}
return self;
}
// and then viewForHeaderInSection and heightForHeaderInSection is implemented below...
#end

You should be subclassing UITableViewController and not UITableView.
when adding a new file to your project (choose : Cocoa touch -> Objective-c class) you should choose subclass of "UITableViewController"
your .h file should look like this
#interface MyTableViewController : UITableViewController
#end
in the .m file you should implement viewForHeaderInsection and heightForHeaderInSection
then go to your storyboard and drag a "UITableViewController" from the objects library.
set the class to MyTableViewController.

Related

Passing value from DetailTableView to MasterTableView

In my DetailTableView users can change the label text (counter) by clicking different buttons. After clicking the back button, the MasterTableView label text should have the same value (counter). Any idea?
To get data from DetailTableView to MasterTableView, you need to create delegate method.
Write this code in MasterViewController.h file
#protocol ContentDelegate <NSObject>
- (void) showMessage;
#end
#interface MasterViewController : UITableViewController
{
}
#property(nonatomic,assign) id<ContentDelegate> delegate;
#end
Implement ShowMessage method in Detail View Controller
- (void) showMessage:(NSString*)message
{
_messageLabel.text = message;
}
Call delegate method from MasterViewController on table cell click
[_delegate showMessage:"Hello !!!"];
First Import MasterViewController in DetailViewController.h file
Then add ContentDelegate in DetailViewController.h file in given form :-
#interface DetailViewController : UITableViewController<ContentDelegate>
{
}
Also set delegate self while calling MasterViewController.
Hope this solution will help you.. Thanks.

How to add multiple tables in UITableViewController

Any one please tell me how to add two tables in uiview controller?
I created a class object in rootviewController as
mainDataViewController=[[MainDataViewController alloc]initWithStyle:UITableViewStylePlain];
And in mainDataviewController taken as UITableviewController
#interface MainDataViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
Now, I Want to add three tables in mainDataViewController.
Please give me some idea for solving this.
follow these steps.
1)Create a tableViewController with xib.
2)Create only two other xib's as given screenshots below:--
3)Drag uitableView from object window.
4)Change its class to TableViewController's class you have firstly created.
5)Connect file owner's view delegate to tableView.
6) And use code as ------
- (void)viewDidLoad
{
MYViewController *FirstTableController=[[MYViewController alloc] initWithNibName:#"MYViewController" bundle:nil];
MYViewController *secondTableController=[[MYViewController alloc] initWithNibName:#"MYSecondController" bundle:nil];
MYViewController *thirdTableController=[[MYViewController alloc] initWithNibName:#"MYThird" bundle:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Or
#import <UIKit/UIKit.h>
#interface MYViewController : UITableViewController
{
IBOutlet UITableView *f_table,*s_table,*t_table; //outlets for different tableViews in xib.
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.view=f_table; // when working with first table.
self.view=s_table; //working with second table.
self.view=t_table; //working with third table.
}
I think you have not searched well. Please have a look at this.
How to use 2 UITableView in a UIViewController?
1) First add tableview on your viewController xib file
2) declare variables for your tableviews
3) synthesize properthies
4) add outlets to your propethies
Now you can use them...

Passing data between UITableViewCell and UITableViewController?

I created master details template project in xcode 4.6 and I added custom cell with 2 textfields. I also created new class which is subclass of UITableViewCell and inside this class I created outlets for text fields. When user types something NSMutableArray is updated and this works fine. Now I am wondering how to pass this array back to MasterViewController (UITableViewController) so that I can use this data to show calculations.
I tried using tutorials for delegates between UIViewControllers but I keep getting errors. Any help is appreciated.
You shouldn't keep data inside the UITableViewCell, as it breaks the MVC.
You need to get a reference of the UITextField on your cell. This is how I do in a login form:
I have a custom cell subclass called TextFieldCell, it has an outlet called textField, I want that my UITableViewController have references to these UITextFields.
First I open my storyboard, set the cell class to TextFieldCell and than connect the UITextField to cell textField outlet. Than I add this to the tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
(…)
if (indexPath.row == 0) {
// Sets the textField of the first cell as the loginTextField.
self.loginTextField = tCell.textField;
} else {
// Sets the textField of the second cell as the passwordTextField.
self.passwordTextField = tCell.textField;
}
tCell.textField.delegate = self;
(…)
}
Now I can access the value of my loginTextField and my passwordTextField. I do that on the tableView:cellForRowAtIndexPath: because that's when I'm creating the cell to add to the table view.
In your case you need to create Protocol:
I just Give Basic Idea for how to Create Protocol
Also Read This Question
#DetailViewController.h
#import <UIKit/UIKit.h>
#protocol MasterDelegate <NSObject>
-(void) getButtonTitile:(NSString *)btnTitle;
#end
#interface DetailViewController : MasterViewController
#property (nonatomic, assign) id<MasterDelegate> customDelegate;
#DetailViewController.m
if([self.customDelegate respondsToSelector:#selector(getButtonTitile:)])
{
[self.customDelegate getButtonTitile:button.currentTitle];
}
#MasterViewController.m
create obj of DetailViewController
DetailViewController *obj = [[DetailViewController alloc] init];
obj.customDelegate = self;
[self.navigationController pushViewController:reportTypeVC animated:YES];
and add delegate method in MasterViewController.m for get button title.
#pragma mark -
#pragma mark - Custom Delegate Method
-(void) getButtonTitile:(NSString *)btnTitle;
{
NSLog(#"%#", btnTitle);
}

Common views in viewControllers - Code re-usability

I have few common views in most of my viewControllers. What I noticed is that I can reuse single code for all viewControllers which is absolutely wise. For this I decided to create a class Utils which has static methods like
+(void)createCommonViews:(float)yAxis:(NSString*)text;
In my case common views are three labels and two images.
Problem : I am not able to add these views from Utils. I am wondering how can I send self as a parameter so that I may add the views from Utils. It may be wrong to add views outside the viewController. In that case what can be the solution? Taking all these views in a UIView, setting return type of Utils method as UIView and then adding UIView to viewController (after calling method from viewController) might solve my problem. But what I am looking for is some other solution.
+(void) createCommonViews:(float)yAxis withText:(NSString*) text toTarget:(UIViewController*) target
{
//create views
[target addSuview:view];
}
But I think returning a Uiview and then adding it in the UIViewController afterwards, is a far better solution.
The method you're attempting is to have your view object as a singleton. This is uncommon at best, at worst a crash waiting to happen. Better design is for each of your view controller classes to have its own instance of your custom view, like so:
#interface MyCommonView : UIView
// ...
#end
#interface MyViewController_A : UIViewController {
MyCommonView *commonView;
}
#property (strong, nonatomic) IBOutlet MyCommonView *commonView;
#end
// Meanwhile somewhere else...
#interface MyViewController_B : UIViewController {
MyCommonView *commonView;
}
#property (strong, nonatomic) IBOutlet MyCommonView *commonView;
#end
Create a viewController that acts as a parent view for all your common stuff, call it CommonViewController then implement this in all the viewcontrollers you want it to appear
-(void) viewDidLoad
{
[self.view addSubView:[[CommonViewController alloc] initWithRect:..];
}
Or alternatively using xib files

didSelectRowAtIndexPath not get called

I have a UITableView inside a UIViewController like so:
.h
#interface OutageListViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITableView *outageTable;
.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected");
}
I have customized table cell:
//EDIT
I have fire UILabel side by side on my customized view, as well as a background spreading the entire area. But after resizing/removing the background image and label I putted on the customized cell, "didSelectRowAtIndexPath" is still not being called.
//END EDIT
#interface AbstractSummaryListViewCell : UITableViewCell {...}
and
#interface FiveColumnSummaryCell : AbstractSummaryListViewCell {...}
This UIView is inside another UIView:
#interface CustomTabBarController : UIViewController {
OutageListViewController *outageListViewController;
And my AppDelegate add this to the window:
[window addSubview:[customTabBarController view]];
Now I'm trying to determine which cell get clicked and didSelectRowAtIndexPath doesn't get called, I have dataSource and delegate connect from the UITableView to File's Owner, in fact the data populates correctly as my "cellForRowAtIndexPath" specifies, any ideas how can I fix this?
Thanks!
I solved it: forgot to check User Interaction Enabled in my customized cell xib. What a fool!
Are the following properties of UITableView all YES?
allowsSelection
allowsSelectionDuringEditing
Edit:
I think Paul is right. The delegate property has some problem. You can check the delegate property of tableView inside -(void)viewDidLoad. As you said, they should be connected to FileOwner in xib. So the following codes won't obtain nil.
- (void)viewDidLoad {
[super viewDidLoad];
// They should not be nil.
NSLog(#"delegate:%# dataSource:%#", self.tableView.delegate, self.tableView.dataSource);
}
It's possible that the view controller has not been connected to the delegate property of the outageTable anywhere.
You can make a quick test... Remove the "big" label and see if the didSelectRowAtIndexPath is called.