Creating UITableView in init failed - ios

I'm trying the following in Xcode 4.3.2. I've created a single view application. My ViewController implements UITableViewDelegate, UITableViewDataSource.
In ViewController.m:
UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, 120, 100)
style:UITableViewStylePlain];
tv.delegate = self;
tv.dataSource = self;
[self.view addSubview:tv];
I implemented numberOfSectionsInTableView, numberOfRowsInSection, cellForRowAtIndexPath and didSelectRowAtIndexPath. It works fine, and displays "welcome" in two rows. But If i place the UITableView creation in the init, then it is not working.
- (id)init
{
self = [super init];
if (self) {
UITableView *tv = [[UITableView alloc] initWithFrame:
CGRectMake(0, 10, 120, 100) style:UITableViewStylePlain];
tv.delegate = self;
tv.dataSource = self;
[self.view addSubview:tv];
}
return self;
}

Two things:
if you're loading from a xib file or storyboard, the initializer used is initWithCoder:.
the view controller's views are not loaded until they're needed, which means viewDidLoad: rather than any init... method. (Though, viewDidLoad: is triggered by a reference to the view property.)

Related

How to add headerView like Navigationbar on ASViewController with ASTableNode without NavigationController?

In my application, I do not use the NavigationController, in general.
In UITableView I would do so, would add
[Self.view addSubview: self.myCustomHeaderView];
But I'm using ASTableNode. If I do something, something similar here:
- (instancetype)init {
_tableNode = [[ASTableNode alloc] init];
self = [super initWithNode:_tableNode];
if (self) {
_tableNode.dataSource = self;
_tableNode.delegate = self;
self.navigationItem = //navigationItem implement
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
navbar.backgroundColor = [UIColor redColor];
navbar.items = #[self.navigationItem];
ASDisplayNode *navBarNode = [[ASDisplayNode alloc] initWithViewBlock:^UIView * _Nonnull {
return navbar;
}];
[self.node addSubnode:navBarNode];
}
return self;
}
Then this node is added as part of the list and scrolled along with the list, and I need it to be as fixed header.
initWith ASDisplayNode rather than ASTableNode,then add table node on VC's node,for example:
#interface OCTextureTableController : ASViewController<ASDisplayNode *>
#property (nonatomic, strong) ASTableNode *tableNode;
- (instancetype)init
{
self = [super initWithNode:[ASDisplayNode new]];
if (self) {
self.tableNode = [[ASTableNode alloc] init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableNode.backgroundColor = [UIColor yellowColor];
self.tableNode.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-NAVIGATION_BAR_HEIGHT);
[self.node addSubnode:self.tableNode];
}
(i) take Class A as base Class.
(ii) take Class B as derived Class having type of class A.
Now in class A Add a uiview on top. that will be considered as navigation.(do anything in that view)
To use same navigation bar: you have to take other all classes as having type of class A.
that's it.

How to add a custom view to JSQMessagesViewController

How can i add a custom view on the top of collection view?
I want to design a custom view using Xcode user interface
I have added my view in storyBoard view controller but the collection view covers it
so I add a custom view in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[self addViewOnTop];
}
}
-(void)addViewOnTop {
UIView *selectableView = [[UIView alloc]initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, 40)];
selectableView.backgroundColor = [UIColor redColor];
UILabel *randomViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 100, 16)];
randomViewLabel.text = #"RandomView";
[selectableView addSubview:randomViewLabel];
[self.view addSubview:selectableView];
}
this code runs successfully but how can I load a view using Xcode interface builder or load a .XIB file
now I do this
#implementation chatViewController
- (void)viewDidLoad {
[super viewDidLoad];
[SVProgressHUD dismiss];
self.collectionView.collectionViewLayout.sectionInset = UIEdgeInsetsMake(70, 0, 0, 0);
self.collectionView.dataSource = self;
self.collectionView.delegate= self;
messages = [NSMutableArray array];
[self addMessages];
self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeZero;
self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero;
self.topContentAdditionalInset = 70.0f;
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleCollectionTapRecognizer:)];
[self.collectionView addGestureRecognizer:tapRecognizer];
self.collectionView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
[self addViewOnTop];
}
-(void)addViewOnTop {
commentsheader *test = [[commentsheader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 70)];
[self.view addSubview:test];
}
but it didn't help
If I am understanding the real problem that you are having is that your customView is covering the collectionView instead of modifying the storyboard just adjust the inset of the collection view.
Add an UIEdgeInset to let you view your messages under the new view Call this in the viewDidLoad()
self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0)
You should try out currentView.bringSubview(toFront: <UIView instance>)
The problem was that the navigation bar hides the view.
But bringing it to the front didn't fix the problem
but doing this fixed it:
in viewDidLoad
self.edgesForExtendedLayout = UIRectEdgeNone;

can't show headerVIew in tableVIew correctly

I create 2 custom tableViews when I use this code:
UITableView *mainTableView=[[MainTableView alloc]init];
[self.view addSubview:mainTableView];
The headerView I create didn't show.
UITableView *mainTableView=[[MainTableView alloc]init];
self.view=mainTableView;
This code shows the right view. What happened to my headerView?
UPDATE:MainTableView.m
+(instancetype)createMainView
{
return [[self alloc]init];
}
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
[self addmainTableView];
[self addHeadImageViewAndPageControl];
[self addTitleLabelAndNavigationButton];
return self;
}
#pragma mark -add view to main view
-(void)addHeadImageViewAndPageControl
{
UIScrollView *headImageView=[[UIScrollView alloc]init];
headImageView.frame=CGRectMake(0, 0, yScreenWidth, yScreenHeight/3);
headImageView.backgroundColor=[UIColor redColor];
self.tableHeaderView=headImageView;
//add pagecontroll
UIPageControl *headPageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(-100, yScreenHeight/3-30, yScreenWidth/3, 25)];
headPageControl.numberOfPages=5;
headPageControl.pageIndicatorTintColor=[UIColor lightGrayColor];
headPageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
headPageControl.currentPage=1;
[self.tableHeaderView addSubview:headPageControl];
}
this is my headerView.
The problem of your code is not set frame for Tableview. Put the code below before you add tableView to self.view.
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
or
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UITableView subclass with default style grouped

I have no clue how can I set grouped style as default to UITableView subclass. My goal is to get grouped TableView (ofc) but by something like that.
TableSubclass *myView = (TableSubclass*)some.other.view
Is this possible? I will be greatful for any advice.
UPDATE
I have ios 6 app with custom UITabBar and custom more section.
My "custom" code
- (void)viewDidLoad
{
[super viewDidLoad];
firstUse=true;
UINavigationController *moreController = self.moreNavigationController;
moreController.navigationBar.barStyle = UIBarStyleBlackOpaque;
self.moreNavigationController.delegate = self;
if ([moreController.topViewController.view isKindOfClass:[UITableView class]])
{
UITableView *view = (UITableView *)moreController.topViewController.view;
view = [view initWithFrame:view.frame style:UITableViewStyleGrouped];
UIView* bview = [[UIView alloc] init];
bview.backgroundColor = [UIColor blackColor];
[view setBackgroundView:bview];
moreTableViewDataSource = [[NXMoreTableViewDataSource alloc] initWithDataSource:view.dataSource];
view.dataSource = moreTableViewDataSource;
}
}
It's working great under 6 but under 7 UITableView don't respond but when I remove
[view initWithFrame:view.frame style:UITableViewStyleGrouped];
It respond again but I lose grouped style.
Are you sure that the table view is not yet configured?
from UITableView Class Reference
When you create a UITableView instance you must specify a table style,
and this style cannot be changed
Have a look how to create and configure UITableView.
Generally you use UITableViewController and then in:
- (void)loadView
{
UITableView *tableView =
[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]
style:UITableViewStylePlain];
...
}

UITapGestureRecognizer EXC_BAD_ACCESS in self-contained UIView subclass

I've used UITapGestureRecognizer tons of times, but in this case I'm getting EXC_BAD_ACCESS when a tap occurs. I think it has to do with the fact that I'm adding it to an Alert-type overlay view. And for some reason the overlay view isn't getting retained even though it's on-screen.
I'm creating the view like this:
HelperView *testHelper = [HelperView helperWithBodyText:#"test text"];
[testHelper presentAtPoint:screenCenter];
The convenience method in HelperView.m looks like this:
+ (id)helperWithBodyText:(NSString*)text
{
return [[self alloc] initWithFrame:CGRectZero bodyText:text];
}
And the rest of the code looks like this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.container = [[AGWindowView alloc] initAndAddToKeyWindow];
self.container.supportedInterfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.overlay.backgroundColor = [UIColor redColor];
self.overlay.alpha = 0.6;
[self.container addSubviewAndFillBounds:self.overlay]; //this fills the screen with a transparent red color, for testing
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissMe:)];
[self.overlay addGestureRecognizer:tap]; //I want to dismiss the whole view if the overlay is tapped
self.content = [[UIView alloc] initWithFrame:CGRectZero];
}
return self;
}
- (id)initWithFrame:(CGRect)frame bodyText:(NSString*)bodyText
{
self = [self initWithFrame:frame];
if (self) {
//TEST frame
self.content.frame = CGRectMake(0, 0, 217, 134);
// Initialization code
UIImage *bgImage = [[UIImage imageNamed:#"helper-bg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(30, 28, 20, 20)];
UIImageView *bgImgView = [[UIImageView alloc] initWithImage:bgImage];
bgImgView.bounds = self.content.frame;
[self.content addSubview:bgImgView];
}
return self;
}
- (void)presentAtPoint:(CGPoint)loc
{
CGPoint newPoint = [self.container convertPoint:loc toView:self.container];
self.content.center = newPoint;
[self.container addSubview:self.content];
}
- (void)dismissMe:(UITapGestureRecognizer*)recognizer
{
//this never happens - I get EXC_BAD_ACCESS when I tap the overlay
}
HelperView is not the view getting displayed and it's not getting retained, but you're using as the target for the gesture recognizer. The AGWindowView property "container" is getting displayed and retained by its superview. Your code needs refactoring since you have this view HelperView that doesn't ever display anything itself, but if you want it to work like this you need to retain HelperView so it doesn't automatically get released. You can do this by assigning it to a strong instance variable.

Resources