My UITableView opens via PopOverViewController , so How can I load one of these cells automatically after app did load ,
the cell selecting process on MainViewController
- (void)setDetailItem:(id)newDetailItem {
if (detailItem != newDetailItem) {
[detailItem release];
detailItem = [newDetailItem retain];
//---update the view---
label.text = [detailItem description];
}
}
and cell selecting in TableViewController :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
myAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
appDelegate.viewController.detailItem = [list objectAtIndex:indexPath.row];
}
I use this code in TableViewController but does not work ! It means after press the the popOver button the code just highlight the cell !!
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
I used above code in different methods like viewDidAppear , viewWillAppear and didSelectRowAtIndexPath and ...
Thank you
When you call selectRowAtIndexPath:animated:scrollPosition:, tableView:didSelectRowAtIndexPath: is not called on the delegate.
From the selectRowAtIndexPath:animated:scrollPosition: reference:
Calling this method does not cause the delegate to receive a
tableView:willSelectRowAtIndexPath: or
tableView:didSelectRowAtIndexPath: message, nor will it send
UITableViewSelectionDidChangeNotification notifications to observers.
So, instead of just calling selectRowAtIndexPath:animated:scrollPosition::
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
you could call the delegate methods manually:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
if ([myTableView.delegate respondsToSelector:#selector(tableView:willSelectRowAtIndexPath:)]) {
[myTableView.delegate tableView:self.tableView willSelectRowAtIndexPath:indexPath];
}
[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition: UITableViewScrollPositionNone];
if ([myTableView.delegate respondsToSelector:#selector(tableView:didSelectRowAtIndexPath:)]) {
[myTableView.delegate tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}
Related
I have a UISearchController and i want to select the first row of the searchResultsController tableview on click of search button in the keyboard.
For that I tried implementing
-(void)searchButtonClicked:(UISerachBar*)searchBar {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.searchResultsController.tableView selectRowAtIndexPath: indexPath animated:false scrollPostion:UITableViewScrollPositionNone];
[self.searchResultsController tableview:self.searchResultsController.tableView didSelectRowAtIndexPath: indexPath];
}
but it didn't work for me!!
Then I tried to make a separate method:
-(void) plotPinOnMapWithIndexPath:(NSIndexPath*)indexpath;
and called it in didSelectRowAtIndexPath and in searchButtonClicked method by passing the indexPaths.But the problem is that the cell doesn't get highlighted the way it gets highlighted when user clicks on it.
This works for me, and is correct:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
optionally try this:
[self.tableView:self.tableView didSelectRowAtIndexPath:indexPath];
or you can use Segue to continue with result:
[self performSegueWithIdentifier:"showDetailView" sender:self];
In prepareForSegue method you pass parameters....
It worked!!
-(void)searchButtonClicked:(UISerachBar*)searchBar {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableview:self.searchResultsController.tableView didSelectRowAtIndexPath: indexPath];
}
I have code as follows:
#import "TestTableViewController.h"
#interface TestTableViewController ()
#property (nonatomic,strong) NSMutableArray *testDataArray;
#end
#implementation TestTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.testDataArray = [[NSMutableArray alloc] init];
for(int i = 0; i < 20; i++){
[self.testDataArray addObject:[NSString stringWithFormat:#"TestString - %#",#(i)]];
}
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.testDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"testCell" forIndexPath:indexPath];
cell.textLabel.text = [self.testDataArray objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)insertNewElement:(UIBarButtonItem *)sender
{
[self.testDataArray insertObject:#"AAAAAA" atIndex:0];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForItem:0 inSection:0]]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
As you see code is very simple.
Storyboard is also standard:
But when I select third cell and insert new cell this happen (Newly inserted cell is without separator line):
I found one solution, but there is a problem with it.First it uses Private API, and second I'm not sure that this is good practice to do:
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subview in self.contentView.superview.subviews) {
if ([NSStringFromClass(subview.class) hasSuffix:#"SeparatorView"]) {
subview.hidden = NO;
}
}
}
Is it possible, that this is bug in Apple?
I would suggest to make little bit change in you simplified version of application with method implementation like,
- (IBAction)insertNewElement:(UIBarButtonItem *)sender
{
//Update 1:
[self.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:selectedrow inSection:0] animated:NO];
[self.testDataArray insertObject:#"AAAAAA" atIndex:0];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForItem:0 inSection:0]]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selectedrow+1 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
} //selectedrow define in your .m file as NSInteger.
May this help you!!
Building on the other answers I would use begin and endUpdates instead
- (IBAction)insertNewElement:(UIBarButtonItem *)sender
{
[self.tableView beginUpdates];
[self.testDataArray insertObject:#"AAAAAA" atIndex:0];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForItem:0 inSection:0]]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selectedrow+1 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
[self.tableView endUpdates];
}
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html#//apple_ref/occ/instm/UITableView/beginUpdates
I am trying to select a row when the table view is loaded. I've found other thread regarding this issue but they didn't solve my problem so I am asking again.
My tableview contains 5 row. And I am doing this:
if ([[dataSource objectAtIndex:indexPath.row] isEqualToString:self.status]) {
[self tableView:[self tableView] didSelectRowAtIndexPath:indexPath];
}
I've also tried cell.selected = YES; and cell.highlighted = YES;.
I am using the following code for custion selection view:
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithRed:180/255.0
green:138/255.0
blue:171/255.0
alpha:0.5];
cell.selectedBackgroundView = customColorView;
Please help me selecting a row programmatically. The row is selected when I tap on the row, but can not select programmatically.
Try with following code
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
Here you need to set indexPath for row that you want to be selected.
You can get this by calling UITableView delegate method which is
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
inside this method you need to check every selected time that same cell are you not tapped.
if ([[myArray objectAtIndex:indexPath.row] isEqualToString:self.same])
{
[myTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
Try calling selectRowAtIndexPath on the table view object:
if ([[dataSource objectAtIndex:indexPath.row] isEqualToString:self.status])
{
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
I have a button that should call the delegate method of tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
when that button is tapped.
Can anyone tell me how to do this.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
A button won't be able to automatically call that method because it has no concept of a table view or index path, but if you create your own method you can call that method directly
- (void)buttonAction:(id)sender
{
NSIndexPath *path = //path for row you want to select
[self tableView:self.tableView didSelectRowAtIndexPath:path];
}
[self tableView:_myTbl didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:_rowNeeded inSection:_sectionNeeded];
//call method, like user selected needed cell
[_myTbl selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
// show selected cell in UI
If you want that when user press the button on your TableViewCell then you fire the Action what's happen when a cell is tapped , then you can do the following.
In your cellForRowAtIndexPath method , add a Gesture Recogniser on your button. Like this ,
cell.myButton.userInteractionEnabled = YES;
cell.myButton.tag = indexPath.row ;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tap setNumberOfTouchesRequired:1];
[tap setNumberOfTapsRequired:1];
[tap setDelegate:self];
[cell.myButton addGestureRecognizer:tap];
Here is the handleTap method
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
NSLog(#"Handle Tap method");
NSLog(#"Row Index : %d" , recognizer.view.tag);
int row_index = recognizer.view.tag ;
// Perform your Action woth row_index
}
And don't forget to add in your header file. Hope it helps.
For select entire row in table view cell check boxes :
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
take "i" value as for loop
i have a menu that contain 6 cells 1st one contain UIViewController the second contain UITableView
when i click the first cell a new page appear,and it contain a button
when i click this button i want access to the second cell from the Menu, that contain a tableView
// use this code for to Refresh or delete or insert TableviewCell
// reloading cell
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationTop];
// inserting cell
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationTop];
// deleting cell
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationBottom];
As much i understand your question you want to move from a tableview to other view on click..So,you have to use code similar to this..
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Detail *img1=[[Detail alloc]initWithNibName:#"Detail" bundle:Nil];
img1.labelString = [titleArray objectAtIndex:indexPath.row];
[self presentViewController:img1 animated:YES completion:nil];
}
Write following coed in your UIButton Tapped Method.
-(void) ButtonTapped:(UIButton *) sender
{
NSIndexPath *indexpathNew = [self.tblView indexPathForCell:(UITableViewCell *)[sender superview]];
NSLog(#"%#",indexpathNew.row);
}
indexpathNew is return indextPath of selected button of cell.
Try my code i might be helpful to you.