Showing/Hiding Rows in Sections of UITableView - uitableview

I am trying to create a UITableView that is set up so that when I click on a section, it hides the rows from the previous section and shows the rows of the newly selected section. The tableView is set up with data from an array of arrays called menuItems. menuItems contains several other arrays including contactInfoArray, clientInfoArray, jacketArray, and shirtArray. In each of these arrays, the objectAtIndex:0 is the Section Title and effectively acts as the header.
So, for example when the app loads, it should first show all rows of the contactInfoArray, but then show the ObjectAtIndex:0 (the title) for each of the other arrays. When I then tap one of those section titles, for example: "Jacket", I need the table to hide the contactInfoArray objectsAtIndexes:1+, but not object 0, while at the same time (or with an acceptable delay) showing jacketArray objectsAtIndexes:1+.
I have achieved the desired result by calling reload data, but I want animation of the change and reloadData does not allow that. I can't find any tutorials or sample code that does this.
Can anybody help me?

Thanks to my good friend Darren for helping me solve this issue!
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section{
return (indexPath.section == currentSection) ? [[menuItems objectAtIndex: indexPath.section ] count] : 1;
}
All that I needed to do was set an immediate variable (in my case "currentSection" which is an int) and set its value in the didSelectRowAtIndexPath: method. Then I set up my table so that row 0 of the section acted as the section title.
Doing this made it so that when I clicked on the section header it would "collapse" the open section and "expand" the newly selected section.

Related

n view controllers or n cells

I have a scenario where the user is transformed to a new view controller vc2 when he presses add button.
depending on where he came from, required fields will change. For instance, if he choose to add a task he should enter title, due date and time, but when he choose to add a person he should enter contact info.
Is it better to have two separate view controllers (vc2a,vc2b) or one table view controller vc2 that contains prototype of all cells for the two case, but every time I call cellForRowAtIndexPath, I have to check from where I come.
for me as a developer it is easier to have different view controller in storyboard especially when n>2, but if that can make a difference in performance things will change.
Thanks in advance.
As per my opinion it is better to do this task in a single view controller, provided you are performing similar next action after adding a task of adding a person.
And instead of putting up the checks in cellForRowAtIndexPath because it is called multiple times do place a check in numberOfRowsInSection.
In numberOfRowsInSection do
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
OMGOrderDetailsSectionType sectionType = [[self.tableViewLayoutArray objectAtIndex:section] integerValue];
if (sectionType == 0) {
if(self.isContactsType) {
return 1;
}
}
else if (sectionType == 1) {
if(self.isTaskType) {
return 1;
}
}
return 0;
}
Rest remains same for cellForRow, do configure both type of cells.

Selecting multiple sections and rows at a time in Tableview

In my tableview I have different number of sections and different number of rows in each section.
I want to select one section, for eg. if I select first section it should select all rows from that section. If I uncheck that section, it should uncheck all rows.
If I select all rows in a section, it should check the section header also, if I uncheck atleast one row in that section, it should uncheck section header.
I can select all rows if I select one section, its working but If I check all rows section header is not getting selected.
Its completely vice versa among sections and rows.
Please Can I get any help for this.
case 1: when select on section header
run for loop to set the property of object (like strSelect=#"1"). set the checkmark image on that section and cell of that section.
case 2: selecting the cells.
on selecting any cell check that all object of that cell are selected or not.
if selected then then set the checkmark image on section and cells of that section
case 3 on deselecting the section header
run for loop to set the property of object (like strSelect=#"0"). set the uncheckmark image on that section and cell of that section.
for case 2 example
NSInteger sectionArrayCount= sectionArray.count;
NSInteger totalSelectedObject=0;
for (Item *obj in sectionArray)
{
if ([obj.strSelectedQuantity isEqualToString:#"1"])
{
totalSelectedObject+=1;
}
}
if (totalSelectedObject == sectionArrayCount)
{
NSLog(#"all cells of that section are selected");
}

NSInternalInconsistencyException when removing rows from a UITableView

I'm receiving an error indicating that I need to update the number of rows in my UITableView after I have deleted a row. I realise that It's because I've not updated the amount of rows in the UITableView, I'm just not sure how to make this change in the code I have below.
Any assistance is really appreciated.
!-- code
NSIndexPath[] rowsToReload = new NSIndexPath[] {
NSIndexPath.FromRowSection(1, 1)
};
dv.TableView.BeginUpdates ();
dv.TableView.DeleteRows(rowsToReload,UITableViewRowAnimation.Automatic);
dv.TableView.EndUpdates ();
!-- error
Name: NSInternalInconsistencyException Reason: Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3)
Your code above is OK. However, you need to call it after you have removed the corresponding items from your data source.
For example, if you are filling your table view from a List<string> (myList) and you wanted to remove the first row:
myList.RemoveAt(0); // remove the item from the list first
NSIndexPath[] rowsToDelete = new NSIndexPath[] {
NSIndexPath.FromRowSection(0, 0)
};
dv.TableView.DeleteRows(rowsToDelete, UITableViewRowAnimation.Automatic);
No need to wrap the DeleteRows call in a Begin/EndUpdates, as you only have one delete action to perform. That is used when you have multiple different actions (eg. DeleteRows + InsertRows) so that the animations are performed smoothly.
#Dimitris thanks for answering my question. I ended up solving the problem by:
calling the reload method on my UITableView
NSIndexPath[] rowsToReload = new NSIndexPath[] {
NSIndexPath.FromRowSection(1, 1)
};
dv.TableView.ReloadRows (rowsToReload,UITableViewRowAnimation.None);
in my UITableViewCell GetCell method
I removed the label, field and button that were included on the first load of the table. Since this screen in my app will be seen only once, when the user loads it for the first time.

how to get rid of the unwanted top cells of table view

I am using custom cell type UITableview.
The custom cell just has two lines of text one below other - key value pair.
The data creating the table is an array sorted based on key.
I need to check whether the top some of the top keys are to be be a part of the table or not.
If not I need to skip the use of those key value pairs and would then use the left array data to create the table. e.g index 0,1,2 may have to be skipped.
When I try to put this condition in cellForRowAtIndexPath: like this
if (key is to be used)
{
cell.key.text = [NSString stringWithFormat:#"%#", [mrow1 etimov]];
cell.value.text = [NSString stringWithFormat:#"%#", [mvalue1 valor]];
}
What this does is that instead of not creating the top 3 cells (say) it creates the cells without actual data of key value but the labels I used in the custom cell for key and value.
What do I change to get rid of the unwanted top cells?
You should change number of rows in tableView:numberOfRowsInSection:. Obviously, if you remove three rows, number of rows will change.
It will also affect the indexPath in tableView:cellForRowAtIndexPath: method. After skipping first three items, indexPath.row = 0 should correspond to third item in item array.
In other words:
numberOfRows = items.count - numberOfSkippedRows
item = items[indexPath.row + numberOfSkippedRows]

Your Second iOS app Tutorial: How Many Sections Do They Want?

In Your Second iOS app, there is a portion that says the following:
After you finish laying out the cells in the table, the detail scene should look similar to this:
However, mine has Section-1, Section-2, Section-3 right above each section. I get the feeling that they want 1 section with three cells.
Can you tell which?
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
This should be your code to have 3 rows and only 1 section like that of image
You incremented the number of sections by mistake instead of incrementing the number of rows. Which you should have done.
Edited to add
You get to this by highlighting the Table View Section in the controller scene and editing the number of rows in the right hand panel.
I picture makes this clearer:
Yes, according to the image its 1 section with 3 rows. In numberOfSections method return 1 and in numberOfRowsInSection method return 3.
They are looking for one section with three cells.

Resources