Selecting multiple sections and rows at a time in Tableview - ios

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

Related

Can we navigate to a new view when clicking a sub menu inside a Expandable table view?

I have a HRMS screen containing many buttons. I want to categorise each button based on a more its functionality like
Attendance->
Attendance Marking
Attendance register
Leave->
Leave Request
Leave OD Authorise
Leave OD Approve
Others->
HolidayList
Leave Balance
Right now my screen look like this
I am trying to implement the expandable list view. The logic is simple that when clicking a sub menu I want to go to the specific View controller associated with it. Although I know how to do this using simple table view using
func tableView(tableView: UITableView!, didSelectRowAtIndexPath
indexPath: NSIndexPath!)
I cant figure out how to implement it while using the expandable table view. I googled about the same but couldn't get a specific solution.
You can use
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{
if (indexPath.section == 0)
{
if (indexPath.row == 0)
{
/*The first row of the first section was tapped.
Put code here that you would like to execute when the
first row of the first section is tapped*/
}
if (indexPath.row == 1)
{
/*The second row of the first section was tapped.
Put code here that you would like to execute when the
second row of the first section is tapped*/
}
}
if (indexPath.section == 1)
{
if (indexPath.row == 0)
{
/*The first row of the second section was tapped.
Put code here that you would like to execute when the
first row of the second section is tapped*/
}
if (indexPath.row == 1)
{
/*The second row of the second section was tapped.
Put code here that you would like to execute when the
second row of the second section is tapped*/
}
}
}
with expandable tableView same as with normal tableviews. Just put your code to call new viewController in the above method for the required tableView row.
One thing to keep in mind is that when you are using multiple sections (which your would be if you are using expandable tableView) then the indexing for the rows for each section starts from zero.
For example there are three sections in your tableView with three rows each, their indexing would be like this
Section 0
Row 0
Row 1
Row 2
Section 1
Row 0
Row 1
Row 2
Section 2
Row 0
Row 1
Row 2
So you need to check for both the section and the row in the didSelectRowAtIndexPath method

Swift: Add empty element in table view (Core Data)

I want to add a 'new element' cell to my UITableView so that after text typing and tapping 'Enter' the new entry must be created. Something like here but only in the end of the list. How can I do this? In my UITableViewController I use NSFetchedResultsController that handles operations with Core Data.
You will need to amend the tableView delegate and datasource methods to include the extra row:
Add another prototype cell to your storyboard to represent your "extra" row. Design it accordingly (subclassing if necessary) and assign it a different cell identifier.
Amend numberOfRowsInSection to return the FRC sectionInfo count plus one
Amend cellForRowAtIndexPath to dequeue with the different cell identifier if indexPath.row is equal to the FRC sectionInfo count (ie. the last row). Configure the cell accordingly.
Amend didSelectRowAtIndexPath to trigger the code to add the new entry.
Check/amend all other delegate/datasource methods (eg. editingStyleForRowAtIndexPath) to ensure they check the indexPath.row and handle the extra row appropriately.

Populate cellForRowAtIndexPath for grouped tableview

I have a uitableview (NOT a UITableViewController) with 2 sections inside the uitableview. For the other methods I was able to rely on a switch case to figure out what section should be populated with the correct data.
switch(section) {
case 0:
return postitiveArray.count
case 1:
return negativeArray.count
default :return 0
}
But cellForRowAtIndexPath doesn't seem to have that option. So how can I populate one section with postitiveArray and the other section with negativeArray?
The tableView(_:cellForRowAtIndexPath:) method supplies you with an NSIndexPath, which has row and section properties. So you just need to get the section property from it to determine which section you're in.

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]

Showing/Hiding Rows in Sections of 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.

Resources