No of sections with no of rows in Tableview - iphone

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

Related

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

Breaking a table into custom sections for iphone application (xcode 4.2)

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.

Number of rows from tableview

I was working with the tableview and I wanted the number of rows (i.e the number of items in tableview). How can I get the number of rows, if available?
Please help me out,
thanks in advance.
take a instance variable in your header file.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
yourHeaderFileVariable = [noOfItemsInArray count];
NSLog(#"total item in table %d",yourHeaderFileVariable);
return [noOfItemsInArray count];
}
or try this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
NSLog(#"total item in table %d",[noOfItemsInArray count]);
return [noOfItemsInArray count];
}
The number of rows in table view for a section is what you are returning from (NSInteger)tableView:numberOfRowsInSection:. So if you return something like [mySource count] from this method then the number of rows is [mySource count].
For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
// to get the count
NSInteger rowCount = [data count];
Now replace [data count] with whatever calculation you have in the delegate method.
You need to implement the delegate method :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [yourArray count];
}
You can then show the result in the table header by using the following delegate method :
- (NSString *)tableView:(UITableView *)resultTableView titleForHeaderInSection:(NSInteger)section
{
NSString *str=[NSString stringWithFormat:#"%i",[yourArray count]];
str=[str stringByAppendingString:#" matches"];
return str;
}
For example, if you have 7 results in the table, the header will display the text "7 matches"
- (NSInteger)tableView:(UITableView *)tablefirst numberOfRowsInSection:(NSInteger)section {
int count;
count=0;
count=[*your array name* count];
NSLog(#"row count=%d",count);
return [*your array name* count];
}
This function returns the number rows(items) in your tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listOfItems count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return numberOfRows;
}

Display some grouped tableview sections according the state of the first section, how?

I would a grouped tableview such that:
when the only row of the first section is associated to a state, say A, I can see only this first section with, possibly, some text under it (in a footer for example);
when this state changes, I would see other sections under the first;
How could I achieve this? Some code/link to obtain something similar?
Thanks,
Fran
No problem, just add some if else logic in all your tableView Datasource and delegate methods.
For example like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (!canUseInAppPurchase || isLoading) {
return 1;
}
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!canUseInAppPurchase || isLoading) {
return 1;
}
if (section == 0) {
// this will be the restore purchases cell
return 1;
}
return [self.products count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = ...
NSString *cellText = nil;
if (!canUseInAppPurchase) {
cellText = #"Please activate inapp purchase";
}
else if (isLoading) {
cellText = #"Loading...";
}
else {
if (section == 0) {
cellText = #"Restore purchases";
}
else {
cellText = productName
}
}
cell.textLabel.text = cellText;
return cell;
}
and if you want to add or remove the second section you could use a simply [tableView reloadData]; or this smoother variant:
[self.tableView beginUpdates];
if (myStateBool) {
// activated .. show section 1 and 2
[self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)] withRowAnimation:UITableViewRowAnimationTop];
}
else {
// deactivated .. hide section 1 and 2
[self.tableView deleteSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)] withRowAnimation:UITableViewRowAnimationBottom];
}
[self.tableView endUpdates];
be careful, you have to change the data in your datasource first. And this code will add 2 sections. But you can adopt it to your needs easily.

Adding data to 2 Sections of UITableView from SINGLE nsmutablearray

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