iOS: Collection view, selectItemAtIndexPath programmatically doesn't work - ios

I don't know why this doesn't work. indexOfIcon is correct, section is correct (checked with NSLog) If I select one everything is correct. But this line doesn't do a thing...why? If selected it should have a blue border. This works great while doing it "manually" but not with code..
- (void)viewWillAppear:(BOOL)animated
{
NSUInteger indexOfIcon;
if(self.mainCategory.icon){
indexOfIcon = [self.icons indexOfObject: self.mainCategory.icon];
} else {
indexOfIcon = 0;
}
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:indexOfIcon inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionBottom];
}

add
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.selected = YES;
and the cell will be selected.
The command [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:indexOfIcon inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionBottom]; tells the collectionView, that the cell is in selected state, but don't set the state of the cell.

After years selection seems to work fine (even in viewDidLoad:)
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[_collection selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionLeft];
successfully triggers cell's -setSelected:

Related

Expanding UILabels inside custom UITableViewCell

I have an old iOS app written in objective c. It has several UILabels that has given fixed x,y,width and height. Now what I want to do is when click on the acessoryView expand the cell and expand the UILabels inside it. Also I want to change the positions of those UILabels. So far I have done cell expanding part in this way.
- (void)expandCell :(UIButton *)sender {
NSLog(#"NSINdex Path-------%li",(long)sender.tag);
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
MetadataTableViewCell *cell = [_metadataListTVCTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
if (newIndexPath == cellExpandedRow) {
cell.isExpanded = true;
}
else {
cell.isExpanded = false;
}
cellExpandedRow = newIndexPath;
[_metadataListTVCTableView beginUpdates];
[_metadataListTVCTableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForItem: sender.tag inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[_metadataListTVCTableView endUpdates];
[_metadataListTVCTableView reloadData];
}
But I have no idea where I can do inner label frame updatings. Please help me. Thanks

How to select a uitableview cell programmatically?

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];
}

disable scrollPosition while scrolling

I have a program that uses scrollPosition= UITableViewScrollPositionTop to display the selected row on top. But when i scroll down and click on any other row, scroller scrolls back to the first selected row. is there any way i can disable the UIScrollPositionTop while i am scrolling. Or Any other solutions please?
This method is called after regular intervals to highlight (select) the rows.
-(void)highlightcell:(int *)rows {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:*rows inSection:0];
[tableview selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
}
You can use UIScrollView delegate to detect when scroll is started and when it has stopped, then use a flag to keep scrolling states as below:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
isScrolling = YES;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
isScrolling = NO;
}
Then in your method:
-(void)highlightcell:(int *)rows {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:*rows inSection:0];
if (isScrolling) {
[tableview selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
else {
[tableview selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
}
}
try to do this..
Self.Table_small.scrollEnabled=NO;

Programmatically setting a UICollectionViewCell as Selected but indexPathsForSelectedItems.count is 0

I programmatically set a custom UICollectionViewCell called CheckCell to be selected as follows:
[self.myCollectionView cellForItemAtIndexPath:indexPath] setSelected:YES];
if ([[self.myCollectionView cellForItemAtIndexPath:indexPath] isSelected]) {
NSLog(#"Selected");
}
NSLog(#"%i",[self.myCollectionView indexPathsForSelectedItems].count);
The first NSLog prints "Selected" leading me to believe the cell at IndexPath is indeed selected. However, the result of the second NSLog is 0. Why is the selected cell's index not being added to indexPathsForSelectedItems?
Here is the answer.
Call selectItemAtIndexPath instead of [self.myCollectionView cellForItemAtIndexPath:indexPath] setSelected:YES];
Example Code:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0] ;
[self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
if ([[self.collectionView cellForItemAtIndexPath:indexPath] isSelected]) {
NSLog(#"selected count %i",[self.collectionView indexPathsForSelectedItems].count);
}
OutPut
selected count 1

how to access tableview cell

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.

Resources