custom cells uitableview - cannot display all the rows - iphone

I'm creating a uitableview with custom cells which I created in Interface Builder. There are currently three cells (UITableViewCell).
I have a UITableView which is on the view.
The code I have is fairly standard, but I cannot work out how to actually SHOW the three custom cells. I am using the cellForRowAtIndexPath method.
My aim is to get a grouped uitableview which shows like the following
Item
- Name
Settings
- alert
- time
So the above indented view should be my grouped uitableview. The problem is, I cannot get it to show using code. The uitableview will ALWAYS be this. I am trying to replicate the settings style screen... so I do not have any arrays to display the uitableview cells.
How do I actually display my three cells? I have the following code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return 1;
else
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0)
return #"Item";
else
return #"Settings";
}
So all I need to know is how to actually display my custom cells inside the tableview!
Thanks for any information. I've searched for AGES in google .. all the tutorials use arrays to display data!

You'll kick yourself.
In cellForRow, use some cascading if statements, like:
if (indexPath.section == 0) {
cell.textLabel.text = #"Name";
} else {
if (indexPath.row == 0) {
cell.textLabel.text = #"alert";
} else {
cell.textLabel.text = #"time";
}
}

Related

Two UITableViewCell with different identifier and different height in storyboard

I have two type of cells that I'll be using my UITableView, so I created two prototype cell with different identifier. I manuelly changed the size but when I compile and run, the two cells have same size.
Is there any way to do it through storyboard and without checking every single time
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
I will have around a good 100 cells at least.
UPDATE: in my case I have a tableview with 3 sections, the first one is small, the second and the third one are bigger.
I think your best option is to use that -tableView:heightForRowAtIndexPath: delegate method you listed and return the height you would like.
You could assign tags to each prototype and use an if/else if conditional.
Or if you have a subclassed UITableViewCell for each prototype you could do something like
...
id cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[CustomCell1 class]]) {
// return aNumber;
} else if ([cell isKindOfClass:[CustomCell2 class]]) {
// return aNumber;
} else {
return aNumber;
}
...
If you know that the sections if what determines the height of the cell, then you can implement it like this:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath {
CGFloat result;
if (indexPath.section == 0) {
result = 160;
} else {
result = 80;
}
return result;
}
Depending on the heights that you need of course.

How to create TableView with different TableViewCell in IOS?

Hi in my app I want to create tableview basen on values dynamically I need to add UItableView cell with different controls like (cell1 with label,label2),(cell2 with label1,textfield2),(cell3 with label1,segment1) etc.I created UItableViewCell in my Xib.But I am not able to load them dynamically .If any one know reply me Thanks
You have to do it on the cellForRowAtIndexPath method. Something like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return cell0;
} else if (indexPat.row == 1) {
return cell1;
} else if (indexPat.row == 2) {
return cell2;
} else if (indexPat.row == 3) {
return cell3;
}
}
cell0, cell1, cell2, cell3 are cells you have created on interface builder and linked with an IBOutlet.

Expand and collapse static table view

I have a static table view in my application. This table view is used for preferences.
A section in the table view only has one cell which holds a UISwitch. When this switch is activated, I want to show the section beneath and when it is not, I want to hide the section beneath. All the sections (also the one which should be hidden / shown) is set up using Interface Builder.
Is there any way to hide or show this section when the table view is static as a static table view doesn't have a data source? Should it be easier, I could also agree to use the same section but add / hide rows from this section when the switch is on or off.
EDIT
I have come closer how to do this.
Setting the height of the cells in the section and the height of the footer and header of the section to 0, I can nearly hide the section. I still have some spacing between the section above and the section below that I cannot figure out how to get rid of.
Does anyone have an idea where this extra spacing comes from? See the photo below.
This is the code I use to nearly hide the section.
/* Will display cell */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2)
cell.hidden = YES;
else
cell.hidden = NO;
}
/* Height of cell */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2)
return 0;
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
/* Height of section header */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 2)
return 0;
return [super tableView:tableView heightForHeaderInSection:section];
}
/* Height of section footer */
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == 2)
return 0;
return [super tableView:tableView heightForFooterInSection:section];
}
This is how the table view looks now. There is still some space I need to hide. The extra space is between the sections labeled "Arbejde" and "Anden".
I got it working using the code in the question. Just set the height to 1.0f instead of 0. It seems that the height only has an effect when it's value is greater than zero.
Reducing the space between sections of the UITableView.
For static cells, I just use this:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let cell = self.tableView(self.tableView, cellForRowAt: indexPath)
if cell.isHidden {
return 0
} else {
return cell.bounds.size.height
}
}
see this tutorial this may helps you www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/
For static cells I found no decent answers either. If you wish to drop sections or expand a cell then this is the code I used.
Create a BOOL Value in your .m
#interface yourClassName (){
BOOL ddMenuButtonPressed;
}
#end
ddMenu means drop down menu.
Next I set the bool value to false in the viewDidLoad method.
ddMenuButtonPressed = false;
I then initialise the ddButton (in storyboard control+drag from your button or switch to your .h file and create action and set name to ddMenuButton) and use this method in my .m file.
- (IBAction)ddMenuShow:(UIButton *)sender
{
if (sender.tag == 0) {
sender.tag = 1;
ddMenuButtonPressed = true;
} else {
sender.tag = 0;
ddMenuButtonPressed = false;
}
//very important that you include these
//they update the view like a viewDidLoad statement without leaving the screen
//if you have any data entry points in your cell like a textfield it **will not** erase that data fortunately
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
Finally we add the following method:
// Handle expanding and minimising cell or section
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//if the section is 0 (and there is only one cell) and ddMenuButtonPressed hasn't been pressed
if (indexPath.section == 0 && ddMenuButtonPressed == false){
//return the height you have it set as in story board (or a number)
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
//if the section is 0 and the ddMenuButton has been pressed
if (indexPath.section == 0 && ddMenuButtonPressed == true){
//change the cell height to 380 or whatever size you want
return 380;
}
//otherwise leave cells as they are
else {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
And that's it. No fancy coding, nice and clean (minus the commenting) and simple and you can use this over and over for as many sections as you like.

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

I keep getting this error, I have set up a custom cell I am trying to display I have connected the datasource and delegates in IB, but I keep getting this error, below is my code... its madness as I have already done this in another project and it works sweet.. and I havent changed anything other than variable names.
2011-05-06 10:40:17.004 instaCode1.3[1500:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
everything has been #synthesized etc...
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
//This method adds headings to my sections
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *title = nil;
// Return a title or nil as appropriate for the section.
switch (section) {
case REG_SECTION:
title = #"Enter Registration";
break;
default:
break;
}
return title;;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return cellRegistration;
}
return 0;
}
The cellForRowAtIndexPath: function returns a UITableViewCell object and thus 0 is not a valid return option. You must return a cell.
If you have a custom cell then you must check the registration of the CELL. This should not be nil under any circumstances.
If its nil then you can use ALLOC, INIT to get the cell registration and return the same.
I think you're asking for trouble with "return 0" from cellforRowAtIndexPath.
I'd put a breakpoint at that line and see if that's where it's breaking, and if so, why is the section != 0?
Is REG_SECTION == 0 (are you sure?)
Edit:
What does this give you in the log?
if (indexPath.section == 0) {
NSLog(#" cellRegistration is %#",cellRegistration);
return cellRegistration;
}

UISearchDisplayController is not displaying any cells

I have a UISearchDisplayController that is properly hooked up in Interface Builder.
delegate = Files Owner
searchBar = Search Bar
searchContentsController = Files Owner
searchResultsDataSource = Files Owner
searchResultsDelegate = Files Owner
When my UITableView calls numberoOfRowsInSection: the correct number is returned.
However, my cells in cellForRowAtIndexPath: don't even reach:
- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tblView == searchController.searchResultsTableView){
NSLog(#"search will go here");
UITableViewCell* cell = [self provideSearchQueryCells:tblView identifer:#"searchQueryCell"];
STSymbol *aSymbol = [self.searchQueryResults objectAtIndex:indexPath.row];
cell.textLabel.text = aSymbol.symbol;
cell.detailTextLabel.text = aSymbol.symbol_title;
return cell;
}
else { ... }
It always goes to the else condition.
I am not exactly sure why.
I needed to create an instance of a UISearchDisplayController instead of using self.searchDisplayController.
use the following. It should work.
if ([tblView isEqual:[searchController searchResultsTableView]]) {
...
}
you should also make sure the search result row count is correct as in :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([tblView isEqual:[searchController searchResultsTableView]]) {
return [self.searchResults count];
}
...
}
This is a guess from this close in on the code, but are we looking at the search display controller itself? Maybe your self.searchDisplayController.searchResultsTableView should just be self.searchResultsTableView.
I can't be sure without knowing your delegates.