I have a UITableView that I build up programmatically and want to preselect a certain section header at startup.
Any hints how I can achieve that?
I'm not quite sure what you mean by pre-select a section header. If you're looking to ensure that the section you are interested in is visible when your app starts you can use this method on UITableView:
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
atScrollPosition:(UITableViewScrollPosition)scrollPosition
animated:(BOOL)animated
If you want to select a row in a particular section you could use this method (which will also scroll to the selected row):
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath
animated:(BOOL)animated
scrollPosition:(UITableViewScrollPosition)scrollPosition
Related
I have a UITableView, and I press the row, but do not release finger, so didSelectRowAtIndexPath is not called yet, right?
How to detect on which row i clicked? in which method?
How to detect that I clicked on table view? Is there any method like rowIsPressed? or onTableViewTouchDown?
There are two options at least:
1) You can subclass UITableViewCell and than use the touchhandling you know from a default uiview. (Like touchesBegan: etc.) / OR add a custom uiview as contentView of the tablecell.
2) Add a gesturerecognizer to each tablecell.
To find out in which row you are, you could give every row a tag (which than refers to the row) in cellForRow:atIndexPath:
try this
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
There is no such method to detect the selected state before lifting the finger... I guess you should override the UITableView class and add a UIGestureRecognizer to it.
And about your second question, I think you are talking about this table view delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
But there is also another method that will give you the current selected row. This is it (directly from the table view class):
- (NSIndexPath *)indexPathForSelectedRow
Hope to have helped!
I'd like to implement a grouped and indexed UITableView without section headers.
I'm using sectionIndexTitlesForTableView:(UITableView *)tableView to return my titles.
I also implemented tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section and returned nil as well as tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section and returned 0.0f.
Both doesn't remove the section headers. There is still a space between the different sections.
I had the same problem and could not find a solution. In the end, I had to subclass UIViewController (not UITableViewController) and use a UITableView with transparent background and the style set to UITableViewStylePlain. Then, I created custom cells for the first and the last row (with rounded corners) and some transparent margin left and right. I can give you more details if you want, but as it does not answer your question straight, I'll just share this link for now.
In some apps such as Cool Iris, LiveShare, i see them using custom views for their plain UITableView section titles. Is there a way to replace the standard section title bar with a custom view?
In order to customize the look of your section header, there are two methods you will probably want to use.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
These are both found in the UITableView delegate protocol. The first will let you create a custom view to display as your section header. The second just makes sure you get the size you want for it so that your view doesn't get cut off
I have created an application in which i have to add users to the sqlite database.
Now the problem is I want the look of the standard iPhone Contact application Where while adding user we have the width of first cell smaller than other cells and the image before that cell..
Can you please give me the idea how such thing is possible.
How to make one cell small and rest others of normal size..
Thanks for any help in advance
There are three UITableViewDelegate messages you can listen for to adjust height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
However, even thought I didn't write Contacts.app I have a feeling they are also using
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
To adjust the views as well. Remember, you don't have to pack everything into a single monolithic custom table view cell. You can create multiple custom table view cells and load them each appropriately depending on the index path.
The contact detail view is a grouped tableview. Each cluster of cells is a section. The top section is a single custom cell with two subviews that look like squashed tableview cells. The left view shows the contact's photo. The right view shows the name.
To reproduce, create a custom UITableView subclass and lay it out like you want either programmatically or in Interface Builder. Then in the tableview delegate's cellForRowAtIndexPath check indexPath.section and return the proper row for the section.
It appears that the Contacts app uses a custom tableHeaderView when presenting the contact details with an image and label. A similar implementation is included in the sample project iPhoneCoreDataRecipes. The RecipeDetailView loads a separate nib in tableViewHeaderView that is used to set the tableView.tableHeaderView property. Have a look at RecipeDetailViewController.{h,m} and DetailHeaderView.xib. When the Contacts app switches to editing mode, the headerView appears to be swapped out for another view that has a button and a tableView with a single cell. This will allow you to set up a separate tableViewDelegate to handle the Name parts of the contact and a delegate to handle the address / telephony details.
To customize cell, we implement class inherit UITableViewCell. Now, to customize header of Section in grouped UITableView (same to below picture), how to do? Please guide to me!
Implement tableView:viewForHeaderInSection: in your table view delegate.
Starting with iOS 5 you also need to implement
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
In addition to
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Otherwise it won't work.
From Apple's reference
The returned object can be a UILabel or UIImageView object, as well as a custom view. This method only works correctly when tableView:heightForHeaderInSection: is also implemented.