How do I select a UITableViewCell by default? - iphone

I have created a UITableView and would like a specific UITableViewCell to appear selected (blue) when the view is loaded.

-(void)viewWillAppear:(BOOL)animated {
// assuming you had the table view wired to IBOutlet myTableView
// and that you wanted to select the first item in the first section
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO
scrollPosition:UITableViewScrollPositionTop];
}
I'm using this technique to select one of two UITableViewCells so the users knows which cell a UIDatePicker below the table view it will effect. This technique is used by Apple in the calendar app when you create a new event and set the dates.

Be judicious using this method, as selecting the row in this way is something Apple suggests against doing to show a "chosen" state.
Instead, consider setting the cell's accessoryType property to something like UITableViewCellAccessoryCheckmark.

You should put it in viewWillAppear.
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO
scrollPosition:0];
If you try to select it in cellForRowAtIndexPath:, then it will not take the required style.

Definitely watch out. I'm sure you have a good reason, but look closely at the Human Interface Guidelines document Apple provides. Apps get rejected for not unselecting table rows. I'd encourage you to find the appropriate section of the HIG and see Apple offers any suggestions.

Use this code to select cell by default in table view, indexPath can be vary according to your need
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[theTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}

Use the UITableViewCell method
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
Info here.

Sometimes, when you're not in a UIViewController, you have no viewWillAppear, and sometimes, you created your UITableView programmatically.
The easy solution is to implement this delegate method:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectedIndex == indexPath.row) {
cell.selected = YES;
}
}
It's does not work in the cellForRowAtIndexPath because the cell is not yet displayed. and the setSelected method is called just when this one is displayed.

Related

Which method will be called when we hold the UITableViewCell

When we select UITableViewCell than - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath called.
But which method will be called if we hold the UITebleViewCell. Guys my problem is that I have created a tableview which contain large cell and in that cell I am setting various view an view's background color.
When I select the cell, the background color of my cell will be gone. I have solved this problem by setting view background color again in didSelectRowAtIndexPath method like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
UIView *vLineview=[selectedCell viewWithTag:1];
vLineview.backgroundColor = [UIColor colorWithRed:(89/255.0) green:(89/255.0) blue:(89/255.0) alpha:1];
}
This done the trick and my view background color will displayed but when I hold the UITableViewCell than it will gone again.
How can I solve this? Do I have to and gesture recognizer to detect long touch and implement my view background method in it? Or there is any other method available for that.
Try with set cell selection style none like this in cellForRowAtIndexPath
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
You can subclass UITableViewCell and override this method:
- (void) setSelected: (BOOL) selected
animated: (BOOL) animated
{
[super setSelected: selected
animated: animated];
// Configure the view for the selected state
}
Is this an issue with the UITableViewCell remaining selected after you tap on the row?
If row selection is not required, make sure you call deselectRowAtIndexPath:animated at the end of your tableView:didSelectRowAtIndexPath method.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Pleasese see method named -(void)beginUpdates
Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath: and indexPathsForVisibleRows) to be animated simultaneously.
for more detail visit apple documentation in below link
UITableView Class Refrence
Also try [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

How to delete a row from table view IOS

I want to delete a row from table view without using the below data source method. How to do it?
- (void) tableView : (UITableView *)tableView
commitEditingStyle : (UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath : (NSIndexPath *)indexPath
How about [UITableView deleteRowsAtIndexPaths: withRowAnimation:]?
(I've linked Apple's documentation linked for you).
#Scar's idea is also good, but that will refresh the entire table and that might be a bit disruptive to the user if they're scrolled to the bottom of the table and the refresh takes them to the top.
Not quite that simple, you have to do this atomically with a UITableView
[_tableView beginUpdates]; // <-- pretty important
long selectedRow = _tableView.selectedRow;
[_tableView removeRowsAtIndexes:[NSIndexSet indexSetWithIndex:selectedRow] withAnimation:NSTableViewAnimationSlideUp];
//then remove your object from the array
[((NSMutableArray*) _searchResults) removeObjectAtIndex:selectedRow];
[_tableView endUpdates];
This prevents delegate methods from being called while the array is being updated.
Remove the row from your model and call -deleteRowsAtIndexPaths:withRowAnimation:.

uitableview autoupadate while scrolling to top

I want to update Uitableview when it will be scroll to top. As in facebook app when we scroll uitableview to top it will show "upadating" and insert new rows in top of table view. I want to give same effect in my app. is it possible to design it using default uitableview methods or we need to customize it.
If anyone is having idea about it please please reply.
You may want to detect when your user scrolled to top:
Several ways to do this:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y == 0)
NSLog(#"At the top");
}
Source: How can I get scrollViewDidScrollToTop to work in a UITableView?
Or using:
scrollViewDidScrollToTop:
Source: http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollingViewContent/ScrollingViewContent.html
You can now fire your events depending on the user's actions:
i.e.
When user scrolled to top, call your UI change using, then call your update logic.
insertRowsAtIndexPaths:withRowAnimation:
Sample and Source: http://www.mlsite.net/blog/?p=466
After update logic has ended, call [tableView reloadData] assuming your datasource is now updated. Optionally you may want to make use of deleteRowsAtIndexPaths:withRowAnimation: to add effects when removing your cell notifying that you are currently updating.
Another way :
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
[self fetchMoreMessages];
}
}
You can use insertRowsAtIndexPaths: to insert new rows at the front of table view. For example,
NSIndexPath *indexPathForRow_0_0 = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *indexPathForRow_0_1 = [NSIndexPath indexPathForRow:1 inSection:0];
[yourTableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathForRow_0_0, indexPathForRow_0_1, nil] withRowAnimation:UITableViewRowAnimationTop];
The above code inserts two rows at the index 0 and 1 in section 0.
Note: You need to customize your dataSource and delegate methods to adjust with these insertions. Important thing is you need to return the correct value from numberOfRowsInSection method. Here, in the above example, your numberOfRowsInSection method should return yourPreviousNumberOfRows + 2, because we added two rows here.

UITable cell selection in a SplitViewController

I have a UISplitViewController with a Table View for navigation. It's similar to the Mail app. When you click on a table view in portrait mode, the popup hides itself. When you click on the nav bar to get the popup back, the selected item no longer appears selected. How can make this item appear selected without re-selecting the item? (just like in the mail app)
In your viewDidLoad method, do you call
self.clearsSelectionOnViewWillAppear = NO; ?
This is how Xcode's SplitView template does it.
Do you have by any change a
[tableView deselectRowAtIndexPath:indexPath animated:YES];
in your didSelectRowAtIndexPath in the RootViewController ?
I've got a solution that works, but it's frustratingly hacky. I have to call selectRowAtIndexPath twice. It seems that cellForRowAtIndexPath is invalidating the selection made in viewWillAppear. It still needs to be called in viewDidAppear, however, so the view scrolls to the proper position before cellForRowAtIndexPath is called.
- (void)viewWillDisappear:(BOOL)animated
{
NSIndexPath *selected = [self.tableView indexPathForSelectedRow];
_selectedRow = selected.row;
}
- (void)viewDidAppear:(BOOL)animated
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_selectedRow inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//initialize cell code here...
if (indexPath.row == _selectedRow) {
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
}
For your table view controller, is -viewWillAppear: called before the pop-up is displayed? If so, you could write it as so:
- (void)viewWillAppear:(BOOL)animated
{
[self.tableView selectRowAtIndexPath:<indexPath>
animated:animated
scrollPosition:UITableViewScrollPositionMiddle];
[super viewWillAppear:animated];
}
Obviously, replace <indexPath> with the proper index path and set the scroll position how you want it. You may also want to pass NO instead of animated to make it appear like it was selectd before the view appeared.

Table View scroll when text field begin editing iphone

I have table view controller in iphone application. Table view has two sections. First section has two rows and second section has one row. Second section has a custom table view cell.
Second section has a textfield which hides when text field begin editing and keyboard pops up. I want this table view to scroll when keyboard pops up.
I tried the following code which I came across on different websites but in vain.
Thanks in advance.
-(void) textFieldDidBeginEditing:(UITextField *)textField {
CGRect textFieldRect = [textField frame];
[self.tableView scrollRectToVisible:textFieldRect animated:YES];
}
atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
-(void) textFieldDidBeginEditing:(UITextField *)textField {
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[self.tableView scrollToRowAtIndexPath:[tableView indexPathForCell:cell]
atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
I have run into this on Static Cell TVC's. There is an issue when overriding viewWillAppear and NOT calling its Super. So if you are doing that, make sure to call
[super viewWillAppear:animated];
at the top of viewWillAppear
You want to use the setContentOffset method of the table view. Determine the magnitude of the vertical scroll (in pixels), and then:
CGFloat verticalScroll = ... your code here ...
[self.tableView setContentOffset:CGPointMake(0, verticalScroll) animated:YES];
My problem was I was adding the table cell containing the UITextField in the
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
function. If you do this the automatic UITableView scrolling doesn't work.
So, you have to do some arithmetic to work out when your last row is showing and put your special UITableViewCell in here along with all the others.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {