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
Related
I am adding a expandale/collapsable UIView below UITableviewCell. I am able to add and hide the view on button action but unable to remove it when view is shown in another cell. I am updating the view height constraint to 0 on hiding.
Here's my code:
-(void)doneButtonClicked:(UIButton*)sender{
CGPoint buttonPosition = [sender convertPoint:CGPointZero
toView:_tableView];
NSIndexPath *indexPath = [_tableView
indexPathForRowAtPoint:buttonPosition];
CustomCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
for (int i = 0; i < [_expandedIndexPaths count]; i++){
if (i != indexPath.row && [_expandedIndexPaths count] > 0){
[self.expandedIndexPaths removeObject:indexPath];
//Animation effect for expanding/collapsing view
[cell animateClosed];
}
}
if (sender.tag == 0) {
[self.expandedIndexPaths addObject:indexPath];
[cell animateOpen];
//[self.expandedIndexPaths removeObject:indexPath];
}else{
[self.expandedIndexPaths removeObject:indexPath];
[cell animateClosed];
}
[_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
The constraint update should also happen in the cellForRow method. Set the constant to 0 there.
If you want the view to appear again for the cell that open it in first
place and didn't close it. Add hidden state boolean var in the cell
class and update every time you update that constant. Then on
cellForRow check the value of that boolean and set the constant
accordingly.
I want to pass NSIndexPath to #selector.
So I used following code, but there is no argument like withObject.
How can I do it?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
// User taps expanded row
if (selectedIndex == indexPath.row){
selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
// User taps different row
if (selectedIndex != -1){
NSIndexPath *prevPath = [NSIndexPath indexPathForRow:selectedIndex inSection:1];
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:prevPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
// User taps new row with unexpanded
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[favoriteButton addTarget:self action:#selector(favoritePressed:) withObject:indexPath forControlEvents:UIControlEventTouchDown];
[normalButton addTarget:self action:#selector(normalPressed) forControlEvents:UIControlEventTouchDown];
[halfButton addTarget:self action:#selector(halfPressed) forControlEvents:UIControlEventTouchDown];
}
-(void)favoritePressed:(NSIndexPath *)path{
}
There is a better way to do this. You can find the touch point on button click and convert that touch point in to your table frame, and through that point you can get indexpath.
-(void)favoritePressed:(UIButton *)sender{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableview];
NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:buttonPosition];
}
Try like this:
-(void)favoritePressed:(UIButton *)sender{
//CustomCell : your custom UITableViewCell class name
CustomCell *cell = (CustomCell) [[sender superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
NOTE:
If you set button in content view of CustomCell this will work for you otherwise you need to add other superview for getting the cell.
The code below may work for certain condition but your button may be a inside other view inside a cell. In that case you may not find the cell.
CustomCell *cell = (CustomCell) [[sender superview] superview];
It is better to use it like this.
- (void)btnFavoriteTapped:(UIButton *)sender {
id view = [sender superview];
while ([view isKindOfClass:[CustomCell class]] == NO) {
view = [view superview];
}
CustomCell *selectedCell = (CustomCell *)view;
NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell];
}
I implemented a collection view in my app.
Collection cell contains a button called delete button for an item.
After deletion a item the tags are not updated, so, If i delete an second item then it will delete a next to it i.e. tag.
My code is as follow:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GradientCell";
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
UIButton *btn_close=[UIButton buttonWithType:UIButtonTypeCustom];
[btn_close setFrame:CGRectMake(50, 00, 18, 18)];
[btn_close setBackgroundImage:[UIImage imageNamed:#"close.png"] forState:UIControlStateNormal];
[btn_close addTarget:self action:#selector(delete_image:) forControlEvents:UIControlEventTouchUpInside];
btn_close.tag=indexPath.row;
return cell;
}
-(void)delete_image:(UIButton*)sender
{
[self.col_view performBatchUpdates:^{
[arr_images removeObjectAtIndex:sender.tag];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:sender.tag inSection:0];
[self.col_view deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
} completion:^(BOOL finished) {
}];
}
The simple answer is DON'T use the row as your tag!!
You should save in the 'tag' any other information, that later can be converted to a row number. For example you can save the image in your 'arr_images', ideally you should store a pointer to the object that is being represented by that cell.
And when you want to remove the object you should use that object to re-construct the indexpath
xxxx = [arr_images indexOfObject:sender.tag];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:xxxx inSection:0];
[self.col_view deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
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:
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.