Adding data to 2 Sections of UITableView from SINGLE nsmutablearray - iphone

I wanted to know how to list data in tableView in different sections BUT from a single datasource.
All examples i saw had number of arrays = number of sections. What i want is suppose I have a 3d nsmutableArray.
If dArray.Id = 1 { //add to first section of UITableView }
else add to Section 2 of UITableView.
Its probably dooable, but i jus need a direction.

Yeah it is possible to do it.
You can have a single NSMutableArray (resultArray) with the entire content in it.
Then in the cellForRowAtIndexPath method you can do it this way
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0)
{
cell.text=[resultArray objectAtIndex:indexPath.row];
}
else if(indexPath.section == 1)
{
cell.text=[resultArray objectAtIndex:(indexPath.row+5)];
}
else if(indexPath.section == 2)
{
cell.text=[resultArray objectAtIndex:(indexPath.row+11)];
}
}
and in numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section {
NSInteger count;
if(section == 0)
{
count=6;
}
else if(section == 1)
{
count=6;
}
else if(section == 2)
{
count=4;
}
return count;
}

Related

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.

No of sections with no of rows in Tableview

How can I manage different no of the rows in different no of sections ? no of rows & sections are not fixed depends on another table view in other class.Suppose If xxx section Header then it contains 5 rows ,another section header contain 4 rows. section header is the account title in table view from other class
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3; or [yourarray count] // 3 sections 0,1,2
}
For different numver of rows in diffrent sections
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the sections.
if( section == 0) // first section with 5 rows
return [[yourarray objectAtIndex:section] count];
if( section == 1) // 2nd section with 4 rows
return [[yourarray objectAtIndex:section] count];
if( section == 2) // 3rd section with 57rows
return [[yourarray objectAtIndex:section] count];
}
In table view delegate method; it has:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return data.count;
}
and
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [data objectAtIndex:section].count;
}
You can set your sections number and rows number in those two methods.
Of course you have to also configure your data to two dimension array.
Hope it helps:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if( section == 0)
return [array count];
if( section == 1)
return [array2 count];
if( section == 2)
return [array3 count];
}
Its Really Simple , Use below tableView delegates-
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// return your another table view's array count here, on which no of sections depends.
}
// Customize the number of rows in the table view.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// use like this
int noOfRows = 0;
if(section == 0)
{
noOfRows = x;
}
else if(section ==1)
{
noOfRows = y;
}
.
.
.
else
{
noOfRows = z;
}
return noOfRows;
}
x, y, z are NSInteger here
Try this
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0){
return 5;
}
if (section == 1){
return 10;
}
//etc.
}

How To Fetch the value Section wise in UItableview?

I am trying to make an app in which i use the Grouped Table view .In that I am creating the sections in which the first letter comes from an array and it is doing perfectly.
the code is below
sections=[[NSMutableArray alloc] init];
for(int i=0;i<[PdfNames count]; i++)
{
NSString *s=[[PdfNames objectAtIndex:i] substringToIndex:1];
NSPredicate *p=[NSPredicate predicateWithFormat:#"letter like '%#'",s];
NSArray *check=[sections filteredArrayUsingPredicate:p];
if([check count]<1)
{
dict=[[[NSMutableDictionary alloc] init] autorelease];
[dict setValue:s forKey:#"letter"];
[sections addObject:dict];
}
}
But now i am not able to get that hoe can i get the names of pdf from array which belongs to their secion Or starts with that alphabet.
You are creating an array sections which is of course empty. You then create an array check based on sections. It will therefore also be empty. [check count] will always be zero. No NSDictionary is going to be created.
Clear?
Try following code its basic logic, I hope you can understand.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0) {
return 10; // Number of rows in section.
} else if (section == 1) {
return 5;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2; // Let say we have two section.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.section == 0) {
// Assign your data
} else if (section == 1) {
// Assign your data
}
}
Similarly you can check for the table section on didSelectRowAtIndexPath: method

How to know if cell is first in section

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.

table view next results

How it is being implemented in table view? To limit the results display in the table view, I need to display eg 10 results, then at the last part of the table cell there's the "next results" part when selecting would load/insert the next set of data into the table view.
Pls. advise me. Thanks
You can make a 2 section UITableView...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (indexPath.section == 0)
return 10;
else {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableView *cell;
if (indexPath.section == 0)
cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"normalCell"];
else
cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"nextCell"];
if (!cell) {
if (indexPath.section == 0)
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"normalCell"];
else
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"nextCell"];
}
if (indexPath.section == 0) {
// Set your normal cells
} else {
// Set your next cell
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
// your normal cells here
} else {
// your next cell here
}
}
Of course this is not the only solution, but it works...