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
Related
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.
I would like to break my table view into sections. I would like to see an example, of a table view broken into 3 sections, and then me being able to choose the index where the sections begin.
So if I have an array of objects, and they populate a table view. I would like to choose the titles of the sections and where the sections begin (so for row 1-13 would be section 1, 13-30 would be section 2, etc...).
I have this so far:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (ingredientListChoice == 1) {
return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (ingredientListChoice == 1) {
return #"Section Title";
}
}
Please let me know if you can show me an example of what I am getting at. Thank you.
Here's a rough way to do it. Basically you'll need to return the correct size of each section from tableView:numberOfRowsInSection: and then set the correct content in your table cell by adding an offset to the row index position when pulling the content from your data array in tableView:cellForRowAtIndexPath:.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0: return 13; break;
case 1: return 17; break;
etc...
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
int offset = 0;
switch (section) {
case 0: offset=0; break;
case 1: offset=13; break;
case 2: offset=30; break;
etc...
}
int arrayRow = indexPath.row + offset;
cell.textLabel.text = [myArray objectAtIndex:arrayRow];
return cell;
}
A cleaner way might be to store the sizes of your sections in an array you store as a property (which you would perhaps set in viewDidLoad) and then numberOfRowsInSection and cellForRowAtIndexPath could read the necessary value(s) from that array, so that if in the future you needed to change the sizes of your sections you'd only have to update one place.
When customizing an UITableView, how can I know wether the cell is the first, last or only one in the section in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath`
method?
Thanks.
To find out if it is the last row in a section, call tableView:numberOfRowsInSection: on the data source delegate (presumably, it's self, since you are in a tableView:cellForRowAtIndexPath: method). If the value returned from that method equals indexPath.row+1, you are at the last row. If additionally indexPath.row == 0, you are at the only row. If indexPath.row == 0 but tableView:numberOfRowsInSection: returned a number other than 1, you are looking at the first row in a section.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger count = [self tableView:tableView numberOfRowsInSection:indexPath.section];
BOOL first = indexPath.row == 0;
BOOL last = indexPath.row+1 == count;
if (first && last) {
NSLog(#"The only cell in section %d", indexPath.section);
} else if (first) {
NSLog(#"The first cell in section %d", indexPath.section);
} else if (last) {
NSLog(#"The last cell in section %d", indexPath.section);
} else {
NSLog(#"Nothing special...");
}
// Do the rest of your work
}
NSIndexPath has all the information you need. Row and section.
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.
I have an uitableview with UITableViewCellSTyleValue1 so i have a textlabel and a detail text label.
The second thing i've got is a NSMutableArray. Now i want to fill the textLabels with the even items and the detailTextlabels with the odd entries.
So for example cell one(zero) gets entry 0 and 1 cell one gets 2 and 3 and so on.
I didn't get it working until now.
That must be pretty easy to do (if you have problems only with distributing array items between corresponding cell labels)...
As every cell "consumes" 2 array entry - number of cells equals [dataArray count]/2:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataArray count]/2;
}
Then in your cellForRowAtIndexPath implementation:
...
cell.textLabel.text = [dataArray objectAtIndex:2*indexPath.row];
cell.detailTextLabel.text = [dataArray objectAtIndex:2*indexPath.row + 1];
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// the usual cell creation/reuse stuff..
// obtain Label1, 2 by viewWithTag..
// index is your instance variable..
index = indexPath.row * 2;
Label1.text = [myArr objectAtIndex:index];
Label2.text = [myArr objectAtIndex:index + 1];
}