I have a TableViewController with my customed TableViewCell, on each row there are two buttons, one is hidden the other is not, when I tap the shown button I want to show the other button, I´ve covered this, my problem is, if I scroll down other hidden buttons are showed because of the Reuseidentifier.
What can I do to only show up the button from the row I`ve selected.
Hope I got clear if not please ask me.
Thank you all.
When you scroll the tableView remaining buttons are hidden because you wrote setHidden method in cellForRowAtIndexPath Without putting any condition so take one NSMutableArray and allocate memory to it. Whatever the index you select just add it MutableArray.Then in cellForRowAtIndexPath Put one condition if that array contains the indexPath don't hide else hide the button.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CutomTableView *cell=[tableView dequeueReusableCellWithIdentifier:#"CellId" forIndexPath:indexPath];
[cell.show setTag:indexPath.row];
[cell.show addTarget:self action:#selector(showButtonACtion:) forControlEvents:UIControlEventTouchUpInside];
NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
//self.selectedRow is NSMutable Array
if ([self.selectedRow containsObject:selectedIndexPath]) {
[cell.hideBtn setHidden:NO];
}else{
[cell.hideBtn setHidden:YES];
}
return cell;
}
-(void)showButtonACtion:(UIButton*)sender{
NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
[self.selectedRow addObject:myIP];
[self.tableV reloadData];
}
When you click on particular button to show another button, just save that NSIndexpath.
For example,
If you are click on 2nd row, then add value in NSMutableDicionary as
[mutableDict setValue:#"YES" forKey:[NSString stringWithFormat:#"%#", indexPath.row]];
And then check for particular indexPath in cellForRowAtIndexPath method.
Whenever you find #"YES" for particular row, implement your desired logic to show or hide buttons.
Hope this helps you.
-(IBAction)firstButton:(UIControl *)sender
{
UIButton *button = (UIButton*)sender;
NSIndexPath *myIP = [NSIndexPath indexPathForRow:sender.tag inSection:0];
CustomCell *cell = (CustomCell*)[self.tableView cellForRowAtIndexPath:myIP];
[cell.button2 setHidden:NO];.
}
Related
I have created a View Controller with a Navigation bar and UiCollectionView. UI Collection View contains custom UICollectionViewCell. Navigation bar contains two UIBarButton items, one is on the left corner - prepared segue to previous page and other item is on the right corner - arranged to delete cell(s) in the UI CollectionView as show in the picture below:
Main Screen
Now I want to remove the selected UICollectionViewCell when UIBarButtonItem in the right corner, is tapped.
This how my cellForItemAtIndexPath method look like:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
self.GlobalIndexPath = indexPath;
MessagesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"messagesCell" forIndexPath:indexPath];
cell.MessageHeading.text = [self.Message_Heading objectAtIndex:indexPath.row];
cell.MessageSubject.text = [self.Message_Subject objectAtIndex:indexPath.row];
cell.MessageContent.text = [self.Message_Details objectAtIndex:indexPath.row];
[cell.Checkbox setHidden:YES];
[cell.Checkbox setChecked:NO];
}
I have tried a solution like Declaring Indexpath as global variable and use it in the button event as below:
#property (strong,nonatomic) NSIndexPath *GlobalIndexPath;
some other code .......
//When Bin Icon(UIBarButtonItem) Clicked
- (IBAction)DeleteMessages:(id)sender {
[self.view makeToast:#"You clicked delete button !"];
NSIndexPath *indexPath = [self.MessageCollectionView.indexPathsForVisibleItems objectAtIndex:0] ;
BOOL created = YES;
// how to get desired selected cell here to delete
MessagesCollectionViewCell *cell = [self.MessageCollectionView cellForItemAtIndexPath:self.GlobalIndexPath];
if([cell.Checkbox isHidden])
{
[cell setHidden:YES];
}
else{
[cell.Checkbox setChecked:NO];
[cell.Checkbox setHidden:YES];
}
}
It's not worked.
For showing the UICollectionViewCell selected as checked, i'm using #Chris Chris Vasselli's solution
Please help me with this. Thanks in Advance.
There are a few steps. First, determine the selected indexPath, but don't assume there is a selection when the method is run....
// in your button method
NSArray *selection = [self.MessageCollectionView indexPathsForSelectedItems];
if (selection.count) {
NSIndexPath *indexPath = selection[0];
[self removeItemAtIndexPath:indexPath];
}
There are two more steps to remove items from a collection view: remove them from your datasource, and tell the view it has changed.
- (void)removeItemAtIndexPath:(NSIndexPath *)indexPath {
// if your arrays are mutable...
[self.Message_Heading removeObjectAtIndex:indexPath.row];
// OR, if the arrays are immutable
NSMutableArray *tempMsgHeading = [self.Message_Heading mutableCopy];
[tempMsgHeading removeObjectAtIndex:indexPath.row];
self.Message_Heading = tempMsgHeading;
// ...
Do one or the other above for each datasource array. The last step is to inform the collection view that the datasource has changed, and it must update itself. There are a few ways to do this. The simplest is:
// ...
[self.MessageCollectionView reloadData];
OR, a little more elegantly:
[self.MessageCollectionView deleteItemsAtIndexPaths:#[indexPath]];
} // end of removeItemAtIndexPath
I have the following situation:
I have a CollectionView whose cells each contain a button
in cellForItemAtIndexPath:, I set a string on each button depending on the cell's [indexPath row]
in the button's action method, I call reloadData: on the CollectionView (because I need to update the information represented by the button texts)
momentarily, while the button pressed is selected, the cells in the CollectionView are displayed "jumbled". (Once the button is released, everything returns to "normal" and the cells appear in the correct order: so ultimately, this is an aesthetic issue as far as the application is concerned.)
So specifically, we have code such as this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell *) [collectionView dequeueReusableCellWithReuseIdentifier: #"MyCellType" forIndexPath: indexPath];
NSString *str = [NSString stringWithFormat:#"%d", [indexPath row]];
[cell.answerButton setTitle: str forState:UIControlStateNormal];
// Set tag on button to mark which row no
// Set button background image depending on some internal data
return cell;
}
- (IBAction)answerButtonPressed:(id)sender {
UIButton *button = (UIButton *) sender;
// look at button tag, decide which button pressed, update internal
// data which might mean we need to change button colours
[_collectionView reloadData];
}
I tried putting the call to reloadData: in a dispatch_async to "delay" the update (I come from a Java background, and putting UI updates in a SwingUtilities.invokeLater() can sometimes help in cases such as this), but this didn't make a difference.
Can anyone assist: am I doing something wrong, or is this just a UI glitch that I have to live with?
instead of reloading the data for the entire collectionView, try pinpointing just the cell that needs updating.
- (IBAction)answerButtonPressed:(id)sender {
UIButton *button = (UIButton *) sender;
// look at button tag, decide which button pressed, update internal
// data which might mean we need to change button colours
NSInteger thisTag = button.tag;
NSIndexPath* thisIndexPath = [NSIndexPath indexPathForItem:thisTag inSection:0];
[_collectionView reloadItemsAtIndexPath:#[thisIndexPath]];
}
I am using [tableview reloadData]; to reload the data in my UItableView, however when I use this I loose my highlight on my UItableVewCell.
I would like to know the best way to reinstate this highlight.
I set a tempIndexPath when the user selects the cell they edit the information then I call reloadData, then inside cellForRowAtIndexPath I use this code to re-highlight the cell however its not working.
if ([tempIndexPath isEqual:indexPath]) {
cell.selected = YES;
}
This code keeps the highlighted selection, and is safe with resorting/inserts/repositioning since it keeps a reference to the underlying object from the model, instead of the index path. It also scrolls to the selection, which is helpful when the updated model causes the selection to be moved out of the current scroll position's frame.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Save the selected object at this row for maintaining the highlight later in reloadData
_selectedItem = [_items objectAtIndex:indexPath.row];
}
- (void) reloadData
{
[_itemsTable reloadData];
//Reselect and scroll to selection
int numItems = _items.count;
for (int i = 0; i < numItems; i++) {
NSDictionary *dict = [_numItems objectAtIndex:i];
if (dict == _selectedItem) {
//This is more reliable than calling the indexPathForSelectedRow on the UITableView,
// since the selection is cleared after calling reloadData
NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
[_itemsTable scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
[_itemsTable selectRowAtIndexPath:selectedIndexPath animated:FALSE scrollPosition:UITableViewScrollPositionNone];
break;
}
}
}
Save the selected row, reload your table view's data and select the row again.
NSIndexPath* selectedIndexPath = [tableView indexPathForSelectedRow];
[tableView reloadData];
[tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
You should know that this is dangerous, because if new items were added to the table view before the selected row, the selection will not be the correct cell. You should calculate how many rows were inserted before the row and adjust the selectedIndexPath accordingly.
i have a TableView with a Custom Cell and a Section Header. The Header shows me the Country and the custom Cell shows me the City.
which look like this:
USA
New York
Los Angeles
Germany
Berlin
Frakfurt
every Cell got a Button. after a press it will navigate and push the city name to the detail view .
The Problem is, that i don't know how to determine the city name after i pressed the button.
I had a solution but it doesn't work anymore:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
StandortCell *cell = (StandortCell *)[tableView dequeueReusableCellWithIdentifier:#"ortCell"];
[cell.detailButton setTag:indexPath.row];
cell.ortLabel.text = [managedObject valueForKey:#"stadtname"];
}
- (IBAction)detailButton:(id)sender {
UIView *contentView = [sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
NSInteger section = cellIndexPath.section;
UIButton * myButton = sender;
int row=myButton.tag;
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:section];
StrasseViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"strasse"];
self.selectedStandort = [self.fetchedResultsController objectAtIndexPath:indexPath];
detail.currentStandort = self.selectedStandort;
[self.navigationController pushViewController:detail animated:YES];
}
when i use my old code it will just show the correct street row in the first section. it didn't detect the other sections(Countries);
When i choose the first city in the thirt section. it will display me the first city in the first section.
I hope you can help me.
use this I hope it will solve your purpose.
Create a button pressed method in your view controller
(IBAction)detailButton:(UIButton *)sender {
CustomUITableViewCellName *cell = (CustomUITableViewCellName *)[[sender superview]superview];
NSIndexPath *cellIndexPath = [YOUR_TABLE_NAME indexPathForCell:cell];
}
For ios 7 add another superview.
Now in this way you will get your IndexPath and you an get the current row by using
indexPath.row or indexPath.section.
You can get the title using sender.title property.
You can use this indexPath in your array to the required details.
You need to send row and section information seperately so make tag as
Tag= row*1000+section.
now from tag get section and row value using row= tag/1000
section =tag%1000
use this value to make correct index path or find some better then 2 mins solution to get that.
Caution its a workaround correct will be to
Subclass UIButton
Add a indexpath property int it
make your class to make button object and insted of tag set indexpath property.
in CustomButton.h
#interface CustomButton :UIButton
#property (nonatomic,assign)NSIndexPath *path;
#end
in CustomButton.m
#implementation CustomButton
#synthesize path;
#end
Use this custom button on your table view cell and instead of tag set path property
All
It might possible the same question asked many times in different manner. But my condition is somewhat different.
I have table where I have around 10 cell, now each cell have different custom controls(i.e. UILabel, UIButton) etc.
Now I want when I click button on cell 0 it change label(custom UILabel and not cell label) value on the same cell, without reload whole table.
yes is your custom cell class you assign the property to UiButton like this
#property(strong,nonatomic) IBOutlet UIButton *btnAdd;
and next thing in this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you write like this code
cell.btnAdd.tag=indexPath.row;
[cell.btnAdd addTarget:self action:#selector(AddMethod:) forControlEvents:UIControlEventTouchUpInside];
and in this class write this code
-(void)AddMethod:(UIButton *)btnAdd
{
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:btnAdd.tag inSection:0]; // if section is 0
customcell *cell = (customcell*)[maintableView cellForRowAtIndexPath:indexPath];
cell.yourlabelname.text=[NSString stringWithFormat:#"%d",[yourlabelname.text intValue] + 1];
}
You can update perticular cell of UITableview just you have store indexPath of that row and use following methods to update it .
[self.myTablviewname beginUpdates];
[self.myTablviewname reloadRowsAtIndexPaths:[NSArray arrayWithObject:button.indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.myTablviewname endUpdates];
Also you can give animation to row as per your convenience.