Cannot see cells when I have Two UITableViews in one UIViewController - ios

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
{...}

Related

why can't I focus on correct uitextfield in the uitableview cell (goes over navigation bar) in iOS, objective c

I'm using a TableViewController with custom TableViweCell to gather user information. inside my custom TableViweCell there are more than 5 textfields. (NOTE : I'm using tableview controller because it helps scroll above keyboard automatically when tap on uitextfield). so when I tap on first uitextfield, automatically focus on it and automatically scroll it to up(stop near the navigation bar bottom line.). then when I tap on another uitextfield it scrolls to up with passing the navigation bar.then user has to scroll down to type on it. what is the problem with this.
should I use different custom uitableview cells for each textfield
tableview .m file implementation.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HoteluserdetailformTableViewCell *formcell = [tableView dequeueReusableCellWithIdentifier:#"hotelformcell" forIndexPath:indexPath];
return formcell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return screenSize.height;
}
Found the solution for this.if we want to use more than one text field we have to return that much of rows in number of rows in section method.
ex : if you want 5 text fields
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
note : use 5 text fields inside the same custom cell does not work for this.

Using UITableViewDataSource methods with static cells

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;

How to make uitableview load many sections at a time?

i have a UITableView to show pictures ,each picture has a name;for some reason, i have to make the name as a section header,and the picture as a section row,the major code is like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [dataList count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
...
return view;
}
the picture is as big as 640*640;
my problem is : every time ,the picture will not load until its top shows in the screen;
i want to load many pictures at a time ,but the fact is a picture is loaded when its top shows in the screen;
Since the delegate method that asks you for a cell won't get called until a new cell is about to get into the view, you have to preload images somewhere outside the delegate method.

Why my Cells doesn't display in my TableViewController?

I've got a problem with my cells in my table view controller.
I've created a table view controller with some cells that the user is able to edit but this cells doesn't display...
Here my code for the view controller :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
return cell;
}
And the header :
#interface AircraftDetailsViewController : UITableViewController
#property (weak, nonatomic) IBOutlet UITextField *AircraftNameLabel;
#property (weak, nonatomic) IBOutlet UITextField *AircraftImmatLabel;
If someone could help me... I don't understand why it is empty...
I've forgotten to add that my cells are "Static cells".
EDIT : Problem sovlved. I just comment these line :
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//
// // Return the number of sections.
// return 0;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//
// // Return the number of rows in the section.
// return 0;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// static NSString *CellIdentifier = #"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//
// // Configure the cell...
//
// return cell;
//}
Now it works.
If you have static tableviews in the storyboard, don't implement any datasource methods. UITableViewController has its own implementation of these which you have overridden in your code above. If you do override the methods, you must call the super implementation, to get the value of your static cells, and then modify as you see fit.
Even if you weren't using static cells, you still wouldn't see anything, because you've told your table view that there are no sections and no rows in the table.
For starters you are returning 0 sections and 0 rows so nothing will be displayed. You should return values > 0 according to your data layout.
After you fix that make sure that "Cell" identifier is defined in IB (In the Cell's properties pane although I believe it's the default name and should be there already).
In your code you specify table have no cells at all.
Return other then 0 value in number* functions
You forgot to declare protocols in .h file. Change this #interface AircraftDetailsViewController : UITableViewController to #interface AircraftDetailsViewController : UITableViewController<UITableViewDataSource, UITableViewDelegate> And you are returning 0 in datasource method which is wrong.
The sections and rows must be a non zero value
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 10;
}
So delete above code and set controller as table data source and table delegate in interface builder with ctrl+drag from tableview to controller. For static cells it's enough.
You told number section and row as zero to tableview via delegate methods. So It shows nothing. According to this, your cellForRowAtIndexPath: never getting called.

UITableView header is not visible

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 #"#";
}

Resources