Using xcode 6.4, Hide/Unhide rows in TableView sections when there are many sections - swift

I have a Table with many sections and each section also has rows. How can I hide/unhide the rows per section? the table will be easier to manage if the user only sees sections and can 'tap' the section to see the row selections.

Implement tableView(_, numberOfRowsInSection) in the table view's data source to return either 0 (to hide them) or the actual value. Then, in the table view's delegate, implement tableView(_:viewForHeaderInSection:) to return a view that listens for taps.

Related

How to avoid scrolling a UITableView when adding new events?

I have a UITableView. It loads sets of events at regular time intervals. I want to be able to scrol the table view to see all events. I had inserted latest events at position number zero in a row. So whenever I reload the data, my table view has moved one row down. So the user can't view exactly what he wants to read. So I want to reload the UITableView without moving its scrolling position. How can I do that?
Note that my event text height should be vary based on events.
You can either use UITableView's -scrollToRowAtIndexPath:atScrollPosition:animated: or, probably better, UIScrollView's -setContentOffset:animated:, to reposition the table view. You'll probably just have to note the current offset, calculate the new offset (based on where & how many rows you added), and set it after you update the table.

Can I drag sections in tableview as I can do it with rows?

I use
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath{
}
method for dragging and dropping rows within and across sections. Can I perform drags with sections as well?
It's probably conceptually (as well as technically) simpler to step back a level to a table view where each row corresponds to the sections of the table you want to re-order. Maybe you could invoke this with a "re-order sections" button, show the section-table-view, re-order, then impose that re-ordering on the detail table view.
I think if you try to do it all inside a single table, you're going to frustrate yourself and your users. My 2 cents.
-Mike
Old question, I know, but I just implemented something that does this myself so I wanted to share the answer in case it helps others who stumble across this.
My basic approach was to add a row representing the section to the top of each section. I also added a "Add a new section" row with editing style of UITableViewCellEditingStyleInsert at the end of each section to allow creation of new sections directly within the view. So, for each section the rows were like:
Row 0 = Section row (visually distinguished by setting different
background color)
Rows 1 to x = normal data rows
Row x+1 = "Add a new section"
(I have a couple of simple methods to translate between "edit mode indexPath" and "real data indexPath".)
For moving sections, the proposed move is checked in the UITableViewDelegate Protocol method tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: to ensure that the section row has moved "far enough" (above or below another section row). The returned indexPath is clipped to either just above or just below the nearest section row (depending on if moving up or down) to give the user a visual indication of where it will be moved.
In tableView:moveRowAtIndexPath:toIndexPath: the section change is made in the data source, then a reloadData message is sent to the tableView to display the change. reloadData is used instead of moveSection:toSection: because with the latter, the data source and display would be out of sync due to the row move and would cause an exception.

Selection reset issue when scrolling a UITableView

I have 15 rows in my table. When I select the first row and scroll to last row of the table, and come to back to the first row, my selection is reset. How can I fix this row selection reset issue when scrolling the table?
How are you selecting the row? Because of the table-cell reuse mechanism, just setting the selected property on an individual cell usually won't work; calling -selectRowsAtIndexPaths:animated: on the table view itself is the correct way to set its selection.

Does UITableView support grouped style with rectangular (not rounded) sections?

I would like to present a UITableView with a basic layout like this:
header view
table row
table row
table row
header view
table row
table row
Grouped style with section header views is the natural way to do this, but I don't want the "shunken (padding on the left and the right), rounded corner" look that grouped sections have. I'd like the look to be like an indexed plain table: table rows are all rectangular and full-width, with periodic header sections to separate the table rows into groups.
Is this possible with a grouped style table? Or can I simulate this with a plain table and custom content views for the section headers? Or is there another way to do this? I'd like to reuse as much of UITableView as possible, without having to write a full custom control.
I'd go for the second option you presented. What you can do is use the delegate methods viewForHeaderInSection and viewForFooterInSection.

how to split a tableview cell into two?

I like to create a cell like contacts's add instant messages table view, with two cells to select: "AIM" and "home".
May be I can use the custom table cell as the cell's content view, but the table view framework always treats
one table view' cell as a whole, for example ,how to respond to did select row at index path message?
I'm pretty sure that Contacts is using a single cell.
The illusion of two cells is created by a custom cell that contains two buttons each configured to look like a tableveiw cell.
To get the same effect as Tweetie's split cell, create a custom cell and add a Segmented Control and create title and detail labels