UITableView with two types of cells - iphone

UITableView contains, suppose, 50 rows. The first row contains one item and every even row index cell contains two items?
for that we need to use two XIB view for each alternate row?
What would be the logic for totalrowcount and cellAtRowatindexpath logic?
Is there any other option?

I imagine your table looks like this:
+---------------------+
| Data 1 | --> Cell Type 1
+---------------------+
| Data 2 | Data 3 | --> Cell Type 2
+---------------------+
| Data 4 |
+---------------------+
| Data 5 | Data 6 |
+---------------------+
. ... .
First of all, you should design two different UITableViewCell nibs for those cell types. Personally, I would use Storyboard, design two dynamic cells with different identifiers and call them when needed. For example:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"Cell Type 1"];
Let's get to tableView's data source methods. I assume that the data is stored in an array called self.tableData. You can build a logic to return the number of rows as follows:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ceil([self.tableData count]*(2.0f/3.0f));
}
And then in your cellForRowAtIndexPath method, return these cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSUInteger objectIndex = floor(indexPath.row*(3.0f/2.0f));
if (indexPath.row % 2 == 0) {
// define cell as Cell Type 1 from nib
cell.yourLabel.text = [self.tableData objectAtIndex:objectIndex];
} else {
// define cell as Cell Type 2 from nib
cell.yourLabel1.text = [self.tableData objectAtIndex:objectIndex];
cell.yourLabel2.text = [self.tableData objectAtIndex:(objectIndex+1)];
}
return cell;
}
Note that, I am assuming your tableData array contains something like NSString objects and you have UILabel's on your cells. The above code fetches the objects from your data source from the corresponding indices and populates your labels.

totalrowcount stands 50
cellForRowAtIndexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// make 2 cellidentifiers here and use it for different nib
UITableViewCell *cell;
if(indexpath.row==0){
//load first nib
}
else
{
//load second nib
}
return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row % 2 == 0) {
//cell type 1
} else if(indexPath.row % 2 == 1) {
//cell type 2
}
}
Above code will help you assign alternate cell pattern

If you have 2 types of cells I suggest use 2 different identifiers and when you dequeue the cells you get the cell need.

Firstly use - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and pass 1 section here as :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
and then
In - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method you will pass total number of rows as a datasource to the tableview.
Ex: if you have to display 50 rows then pass 50 rows as :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 50;
}
And in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you will pass the cell as per the need
Ex:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row %2 ==0)
{
// First alternaate cell which you need to display at even index
return ALTERNATE_NIB_1;
}
else
{
// Second alternaate cell which you need to display at odd index
return ALTERNATE_NIB_1;
}
return nil;
}
Hope it helps you.

Related

NSInternalInconsistencyException being returned from tableView:cellForRowAtIndexPath: when using Storyboard

I am fairly new to Objective C and to iOS development in general, and I'm still trying to get a feel for it. But I am trying to make a simple iOS app with (as of right now) a UITableViewController as the main page using Storyboards. I am just trying to get the cells to appear. My code is:
- (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 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cervical_patterns = #"cervical_patterns";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cervical_patterns"];
return cell;
}
I have closely followed the second iOS Birdwatching tutorial app (link) and have previously gotten a UITableViewController to be viewable in the simulator. But I can't figure out why this is not working like the birdwatching app.
The error I am getting is :
'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
What am I doing wrong?
So you are using a Storyboards and a UITableViewController. This means you need to:
Go to your Storyboard
Click on the first cell in your Table View
Make sure the Utilities (window on the right) is showing
Click on the Attribute Inspector
Change "Style" from "Custom" to "Basic" (use Custom if you have customized the cell)
Click on Identifier and make sure it is the same as in your cellForRowAtIndexPath method (cervical_patterns)
Also check that you have set the class correctly:
Go to your Storyboard
Click on the black bar under your Table View
Make sure the Utilities (window on the right) is showing
Click on the Identity Inspector
Make sure your custom class is selected in the drop down
I have just done this in XCode and it works perfectly (note that no (cell == nil) code is required). Exact code below:
- (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 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cervical_patterns";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the text and graphics of the cell
return cell;
}
You need to actually create the cell, like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cervical_patterns = #"cervical_patterns";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cervical_patterns"];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cervical_patterns ] autorelease];
}
return cell;
}

How to Handle CellForRowAtIndexpath method

I had one table view in which I had 15 row in one section.
so when the table view get loaded it shows only first 9 rows, I checked it with method CellForRowAtIndexpath method but I want to get all 15 rows to be get called in this method for the viewload method.
please help me on out.
thanks in advance....
it depends how much rows you set from
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if you have an array then return
[arr count]; as number of rows and if you want static rows then use return 15; in this method
You use these method or if you want all your cell on front of first time view then you should decrease the height of the cell using last method.And also check your array how much values he have.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Array count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier]
autorelease];
}
cell.textLabel.text=[Array objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;//use this method for cell height
}

UITableViewCell width too narrow (in grouped style)

I want a custom look for my UITableView with images for the header, footer and cells. For that matter, I return a UIImageView for the header and footer of the table, and I also set a UIImageView as the backgroundView of the cells.
The problem is that image of the cells is narrower than the header/footer. This seems to be the default behavior of the grouped style of UITableView. I need a grouped style so that the header won't overlap with the cells when scrolling.
Is there any way around this problem?
sorry my poor english, i could understend you wrong. But if there was not mistake:
if you will not find "right" solution, you may try this:
(this is not the best way to do what you whant, but it is the only one I can offer)
all you need is make your table view with only ONE section, without header. Then, just make every "raw" wich must be header of anothe section with different parameters:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return <number of all raws in all sections plus number of sections, to make some raws - sections>;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
for (int i = 0; i < (number of sections); i++) {
if (indexPath.raw == i*(number of raws in section i)) return (heigth of header);
}
return (heigth of raw);
}
//Configure cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
for (int i = 0; i < (number of sections); i++) if (indexPath.raw == i*(number of raws in section i)) {
//set properties to headers
return cell;
}
//set properties to raws
return cell;
}

UITableView Putting array objects into sections

Hey guys I need to know who to put these array objects into two separate sections, that means one section for the red cell and another section for the blue cell. Would really appreciate your help been stuck on this all day now. Here is my code:
-(void)viewDidLoad {
[super viewDidLoad];
//Initialize the array.
array = [[NSMutableArray alloc] init];
[array addObject:#"Red"];
[array addObject:#"Blue"];
self.ViewTable.backgroundColor = [UIColor clearColor];
}
You need to implement:
return number of sections:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return number of rows for the requested section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return correct cell by reading both indexPath.row and indexPath.section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
So for red you would look for a cell request that has indexPath.section equal to 0 and indexPath.row equal to 0. blue would be indexPath.section equal to 1 and indexPath.row equal to 0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2; // This returns 2 sections
}
Updated:
In the cellForRowAtIndexPath
NSInteger section = [IndexPath section];
if (section == 0)
// write your code for red here
if (section == 1)
// write your code for blue here

date wise sorted uitableview

I am developing an iPhone application .
In the application I want to show the uitableview data sorted on the date field :
Suppose I have a Person object with fields name,birthdate,phone number etc.
Now I have array of Person and I am sorting that array on date field.
Now I am not understanding that how to handle these two methods ;
(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
i.e
How to count date wise objects and determine ?
Assuming you have one section and a sorted NSArray or NSMutableArray called _personArray:
- (NSInteger) numberOfSectionsInTableView:(UITableView *)_tableView {
return 1;
}
- (NSInteger) tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)_section {
return [_personArray count];
}
- (UITableViewCell *) tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)_indexPath {
Person *_person = [_personArray objectAtIndex:_indexPath.row];
NSString *_cellIdentifier = [NSString stringWithFormat: #"%d:%d", _indexPath.section, _indexPath.row];
UITableViewCell *_cell = [_tableView dequeueReusableCellWithIdentifier:_cellIdentifier];
if (_cell == nil) {
_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:_cellIdentifier] autorelease];
}
_cell.textLabel.text = _person.name;
_cell.detailTextLabel.text = _person.birthdate;
return _cell;
}
If you require multiple sections, split your array into multiple NSMutableArray instances, each containing Person instances associated with a specific section.
Then modify -numberOfSectionsInTableView: to return the number of sections (a count of the number of section arrays), as well as the other two methods to: 1) get element counts within a section array and; 2) to recall the right Person for a given indexPath.section and indexPath.row.