SWTableviewcell - call animation method without swiping or selecting - uitableview

I have been using SWTableViewCell in a project but I wanted to make the user aware that the swipe function is available on a particular UITableview. I am hoping to do this by momentarily revealing the swipe view animation of the first tableviewcell when the tableview is loaded. So for example the tableview will load, the right utility Button will open for cell 0, then momentarily hide again - thereby showing the user that there is an option to swipe the cell to reveal a 'right utility button'. I am thinking that could achieve this by using a simple timer and by triggering the appropriate methods.
However, I need some direction as to which methods to call for this to work. I have tried the following but this has not actioned the animation itself:
NSIndexPath* indexPath= [NSIndexPath indexPathForRow:0 inSection:0];
GenericTableViewCell6 *cell = (GenericTableViewCell6 *)[self.tableView cellForRowAtIndexPath:indexPath];
[ self swipeableTableViewCell:cell scrollingToState:2];
I have tried tweaking this code to see if i can action the animation - but again this tweak has not worked for me.
NSIndexPath* indexPath= [NSIndexPath indexPathForRow:0 inSection:0];
GenericTableViewCell6 *cell = (GenericTableViewCell6 *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell showRightUtilityButtonsAnimated:YES];
I would be very grateful for any guidance or input on this. please message if you need any further information.
many thanks
james.

After a few strong coffees I figured this one out. Here is my solution:
- (void)tableView:(UITableView *)tableView willDisplayCell:(SWTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row== 0) {
[cell showRightUtilityButtonsAnimated:YES];
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[cell showLeftUtilityButtonsAnimated:YES];
});
}
}

Related

Enabling UIButton in TableViewCell enables in more Cells

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

Is there any method like tableViewWillEndDisplayingCell in iOS for UITableViewDelegate?

I would like to fade cell in table view in my iPhone application before it disappears. When it is about to appear I use
- (void) tableView: (UITableView *) tableView
willDisplayCell: (UITableViewCell *) cell
forRowAtIndexPath: (NSIndexPath *) indexPath
to change alpha from zero to 1 with 0.3 sec animation.
I would like to to the same but from 1 to zero when it is about to disappear. Is it any method similar to the one above or how else can I do that?
I don't want to use standard iOS UITableViewRowAnimationFade because I don't want any other transitions while reloading table.
EDIT:
I am reloading content of the table view. Let say that I have a button which I click and it reloads data in my table. That is when I want to fade out existing cells and fade in new ones.
The controller I am using is a basic UIViewController implementing UITableViewDataSource UITableViewDelegate UIScrollViewDelegate protocols.
I found a solution.
Just before calling [self.tableView reloadData] I am animating all visible cells to fade out. I get all visible cells and iterate over them by
for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows])
{
// here I animate cells to fade out
}
(thanks #AlexKoren for showing me the way to iterate over visible cells).
Because I was using [self.tableView reloadData] without any additional iOS built-in animation the cells were just disappearing. So now they are fading first and then my new cells are fading in with animation defined in
- (void) tableView: (UITableView *) tableView
willDisplayCell: (UITableViewCell *) cell
forRowAtIndexPath: (NSIndexPath *) indexPath
Important: I am reloading table view after animation is finished. If I called [self.tableView reloadData] just after iterating over cells it would reload table before animation was displayed. Because I know how long it takes to finish animation I use
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
{
[self.tableView reloadData];
});
with delay calculated before.
This is a bit of a hack, but should work:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//loop through visible indexPaths
for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows]) {
//get each cell
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//get its location
CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:indexPath];
//set its alpha based on whether or not its going off the screen at the top.
cell.alpha = 1 + rectInTableView.origin.y / rectInTableView.size.height;
}
}
Have you tried this one?
- (void)tableView:(UITableView *)tableView
didEndDisplayingCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
I think this is what you want.

In Objective C UITableVIew how to retain scroll position after calling reload

In Objective C UITableVIew how to retain scroll position after calling reload
While selecting a cell I call reload function of the UITableView but after reload I want the scroll to maintain its position (i.e, Position where the cell was tapped)
You can use reloadRowsAtIndexPaths:withRowAnimation: instead of reloadData.
You could do something like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
Also if you want to scroll to some particular position, you can use this.
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
Two lines and you get the desired result:
NSArray *indexPaths = tableView.indexPathsForVisibleRows;
[tableView reloadRowsAtIndexPaths:indexPathswithRowAnimation:UITableViewRowAnimationNone];'

TableViewCell subtitle won't change unless the cell is tapped

The cells in my table have a subtitle set that will show some extra information loaded from a web server. When the app loads the subtitle will just say "Loading..." and then when the response is received, and parsed the cell is updated.
The problem is, unless I tap on the cell the subtitle will stay at "Loading...". As soon as I tap on it it updates to the correct subtitle.
Here I initialize the cell, and set the temporary subtitle while the http request is performed
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Setting the tableviewcell titles
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.detailTextLabel.text = #"Loading...";
cell.detailTextLabel.enabled = NO;
return cell;
}
I've tried making calling the request method in different places:
willDisplayCell and in cellForRowAtIndexPath.
The method that gets the data from the web server uses an asynchronous NSURLConnection which when a successful response is received I update the cell subtitle text using:
// Map Reduce the array used by the TableView
for (int i = 0; i < [self.routes count]; i++) {
if(cellMatches){
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
cell.detailTextLabel.text = #"Data received!";
cell.userInteractionEnabled = YES;
cell.textLabel.enabled = YES;
cell.detailTextLabel.enabled = YES;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
I know that you can reload a specific cell using tableView reloadRowsAtIndexPaths but that doesn't seem to work when I implement this code:
[self.tableView beginUpdates];
// Change cell subtitle
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
I have a timer set up to call the request method every 30 seconds, when that is called it works no problem and updates the subtitle right away without me having to tap it. So I think the problem is that the cell isn't initialized or maybe it's being reinitialized after the web request is made. But I don't call reloadAllData during this method.
What you need to do is update your cellForRowAtIndexPath so it checks for the data. If the data is available, set the subtitle to the data, otherwise show "Loading". This requires that you have some sort of data model that stores the data when it is received.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
BOOL enabled = YES;
NSString *subtitle = ... // get the value from the data model
if (subtitle) {
cell.textLabel.text = ... // whatever value goes here
cell.detailTextLabel.text = subtitle;
cell.detailTextLabel.enabled = YES;
} else {
// There's no value for this row yet - show "Loading"
cell.textLabel.text = ... // whatever value goes here when loading
cell.detailTextLabel.text = #"Loading";
cell.detailTextLabel.enabled = NO;
}
return cell;
}
Be sure you set the same set of cell properties in both halves of the if/else statement as needed.
When you get new data and update your data model, simply call reloadRowsAtIndexPaths: for the proper cell path. The above code will then properly update the cell.
The code you have now to update a cell should be removed since it is not the proper way.
Try this:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
Instead of:
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
Alongside with rmaddy's solution, I also needed to add one important thing that I found in a similar question:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
Fixed the issue completely.

how can i acess a cells object if selected

i seem to be having some difficulty trying to access a particular cell, i just need to change one of its objects when i select it in the tableview. There must be a way more efficient way than just calling reloadData all over again. Because thats the only way i could make a table cell look different, by rebuilding it when i select on one. Thanks for all the help :)
[self updatePreferences];
[tableView reloadData];
This is all i have atm on the method where it handles the interaction with table cells.
Use the following code.To reload a single cell.
NSArray *array = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:section]];
[self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];
You could also make the cell a custom one and add a method to it like update or refresh.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell refresh];
}

Resources