I want to add search bar to UITableView header, it's visible on IB but not on emulator after running
Check that you are actually calling- initWithNibName: bundle: method properly.
Actually headers are the part of section in table view, so ur view controller class must conform to UITableViewDelegate protocol and must implements the method
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
and for customization of ur header view use
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
example
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 25.0f;
}
or you can use UITableViewDataSource method
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"#";
}
Related
I've created classes both to be the dataSource and delegate of a table view. While the cells are appearing properly, I am finding that this method:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
is not getting called.
What would be a reason for this?
All I could think of was verifying that I did not implement:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and I DID implement:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section`
Is there any other reason the method would not be called?
First of all, do not mark this as duplicate right away. I know that using UITableViewDataSource methods with static cells is not recommended and usually results in a crash, but I want to know the behavior that leads to this.
In the IB, I have a UITableViewController with 10 different static cells in a single section. I want to be able to rearrange and separate them into multiple sections without using prototype cells, since they will never be reused. Below are my implementations for the data source methods:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.indexMappings.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [(NSArray*)self.indexMappings[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell;
NSIndexPath *mappedIndexPath = [self getMappedIndexPathForIndexPath:indexPath];
cell = [super tableView:tableView cellForRowAtIndexPath:mappedIndexPath];
return cell;
}
The problem here is, if self.indexMappings.count is greater than the number of sections in my static table, the app crashes. But if its less than or equals, the mappings work just fine. For clarification, self.indexMappings is a two dimensional NSArray*. Anyone know the reason for this behavior?
I found out that the way to do this is to also implement some of the UITableViewDataSource methods as well. When the app crashes, it can be seen from the call stack that the methods causing the trouble are methods that determine the header or footer height, or header or footer views, etc. After implementing the following methods to return some default values, the problem was solved.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
First of all this is not a duplicate as I have read through very similarly titled questions but they do not give me the correct answer!
So what I have done is:
First of all create a Table View with 13 unique (contain different buttons) prototype cells where the table views content is "Dynamic Prototypes". I then created a UITableViewCell class called blurCell.
I can see the new/second UITableView but there are no cells within it?
I currently have (for the first UITableView) the code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 25;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
}
But I did this because I was using a .xib for this table.
Do I need this kind of code for the new/second UITableView as I have done everything manually in the storyboard?
Please help?
In the ViewController.m (ImagesTableViewController) that contains these two tables I have the code:
#interface ImagesTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Which is linked to both tables
Each tableView should be declared as its own property with the delegate/data source set to self most likely in viewDidLoad
You need to do something like this for the delegate/data source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView==firstTableView) return 25;
else if(tableView==secondTableView) return 2;
else return 15;
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if(tableView==firstTableView) return 1;
else if(tableView==secondTableView) return 2;
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{...}
I have a UItableViewController inside a containerView and when I touch in one of my cells, it doesn't call didSelectRowAtIndexPath. However, if I long press, this method is called normally.
I've used storyboard and my delegate and datasource are my tableViewController.
Here is my tableViewController code:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return nuberOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return nuberOfSections;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return heightForHeader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MWSideMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"menuCell"];
// cell settups
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Some action done
}
There could be three reasons
delegate is not set
Could be a typo in the didselectrowatindexpath
gesture recognizers might be used in container VC or containing VC
same problem happened with me because I have added a tap gesture recogniser over it. `
If you have used any gesture try removing it
` and check if it causing the problem.
I have a project in which I need to put two UITableView on one UIView.
I know it needs to set <UITableViewDelegate,UITableViewDataSource> and can function below:
-(NSString *) tableView:(UITableView *) tableView
titleForHeaderInsection:(NSInteger)section
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
but I do not know if these can process two tableviews (different from 2 sections).
It looks like you're asking if one object (maybe a view controller?) can have two UITableViews both using it as their delegate. Yes, a view controller can be the delegate for multiple table views-- that's why all of those methods pass in a UITableView* as their first argument; it's for you to use to figure out which one is which. You should keep a couple instance variables (IBOutlets probably) in your view controller so you know which is which and you can act appropriately.
Cheers,
Interdev.