When the user taps a UITableViewCell I want an image in that cell to make a little bouncing animation to provide user feedback. That animation works exactly like I want except it is not always triggered when I'm tapping a cell. I'm using Facebook POP animation framework.
#import <POP/POP.h>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"customcell" forIndexPath:indexPath];
POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1,1)];
scaleAnimation.velocity = [NSValue valueWithCGPoint:CGPointMake(5,5)];
scaleAnimation.springBounciness = 20.f;
[cell.bounceimage pop_addAnimation:scaleAnimation forKey:#"scaleAnimation"];
}
Do I need to reset the animation each time a cell is tapped, or anything else I'm missing?
Thanks in advance
The problem is that you are creating (or re-using) a cell every time didSelectRowAtIndexPath is called because you are using dequeueReusableCellWithIdentifier. Instead the first line of you implementation should be:
MyCustomCell *cell = (MyCustomCell *)[tableView cellForRowAtIndexPath: indexPath];
Related
Probably a simple question, but I can't find the answer.
When I set my textLabel it overwrites my Detail Disclosure button that I have in my content view of the cell, put in through the storyboard. Now it only shows when the cell is selected. My code looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"accountCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
User *user = [arr objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = [user username];
return cell;
}
When I take away cell.textLabel.text = [user username]; it shows all the time, as I want.
Any tips? Guess I gotta put some code in for my button...
Figured it out...
To understand this question, you need to understand that a UITableViewCell only has three spaces - imageView, textLabel and accessory. When you set the textLabel it overwrites everything you've created in the content area in storyboard. I had to set a a new class, with subclass UITableViewCell and set the custom IBOutlets in that one, then link in the custom class to my TableView.
My project is a large one, and I'm not sure which code snippet is causing the problem, so I'll just describe the problem. I have an initial view that is a UITableView. This view has a navigation bar, in which one of the buttons is an "edit" button. Pressing the "edit" button sends you into a modal view controller in which there is another UITableView. This UITableView has the attribute that it is a checklist table view (in which multiple items can be selected [checked] or deselected. Once you are finished choosing your items, it saves the array of chosen objects into an NSUserDefault. Now, you are back at the original page where the array of things you chose should be displayed on the UITableView. I change the array that is feeding the UITableView it's data to the array grabbed from the NSUserDefaults. I then call [tableView reloadData] and nothing changes. I would really appreciate any tips. If you guys think you know what part of the code is causing me grief, please respond and I'll post it. Thanks (and by the way, I know I should be making the main view controller the delegate of modal view controllers). Thanks in advance.
Call [[NSUserDefaults standardUserDefaults] synchronize]; after you make changes in the modal view controller. This will save the changes.
Be sure to update the cell content each time the table row is reloaded.
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Bleh *data = (Get your data for this row);
cell.textLabel.text = data.myValue;
cell.imageView.image = data.myImage;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
Try to reload your tableview on viewwillappear , when you back from modelviewcontroller method calls
- (void)viewWillAppear:(BOOL)animated{
[yourTableView reloadData];
}
I have a UIView that gets added to my UITableView.view as a subview. It looks great and works fine, but when I scroll on my UITableView, the subview gets chopped up and disappears but only part of it (mostly the part that scrolls off). I can get it to come back on by calling:
[self.view bringSubviewToFront:self.myView];
but I'd rather not have to call this whenever the UITableView scrolls. Is there something I'm doing wrong? Thanks in advance.
Try this ..
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
}
}
It's hard to tell what you end goal is but adding a subview to UITableView is kind of counter intuitive. The UITableView manages it's subviews (UITableViewCell's) and they are probably taking precedence over your subview. Are you looking for backgroundView?
I am trying to sync a UITableView with a UILabel to make sure they show the same data; of course, things will be different in the end, but for testing this is what I need to do.
See the arrow? I want that middle cell (from px44-88) to show the cell.textLabel.text in a UILabel when it is the "middle cell".
I tried using - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath but I was having so many problems I figured I'd come here to ask if anyone has a better way of doing this. I'm not sure if it would make a difference or not but I am using NSFetchedResultsController to populate my UITableView.
UITableView is a subclass of UIScrollView, so probably you can use UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
member to detect which cell is currently in the middle.
I.e. assuming you have a plain table with no sections, and tableView is an outlet for the table, label is an outlet for the label, than this will function in your controller will work:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
int row = (scrollView.contentOffset.y + tableView.frame.size.height / 2) / 44;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
label.text = cell.textLabel.text;
}
Of course, you need to do some scrolling to make it work ;)
You can use visibleCells method to get layout of visible cells for non-plain table and use it to detect cell in the middle of the table.
I have a couple of rows in a table view which I like to edit.
I thought that setEditing method would give me a Edit and Delete button, but it only shows a Delete button. Because I don't have a detail view controller that's going to be pushed in didSelectRowAtIndexPath I thought I could show a couple of editing elements in the selected cell.
I need to add three buttons to specify priority on assignments: Low, High and Medium priority. This means that I have to change the height of the selected cell to make room for these buttons, I think that's rather easy to do.
What I'm wondering is if this is the correct path to choose?
I have done quite a lot research today without finding examples of how other have solved editing in a UITableViewCell. If you edit a contact in the Contacts app in the iPhone the UITableViewCells changes to enable quick and easy editing, that's what I'm looking for.
So what do you have for tips for me regarding this question?
Edit #1
My code in didSelectRowAtIndexPath is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *selectedCell = (Cell *)[tableView cellForRowAtIndexPath: indexPath];
UIButton *btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 50, 100);
btn.titleLabel.text = #"Set this item to High Priority";
btn.backgroundColor = [UIColor greenColor];
[selectedCell.contentView addSubview: btn];
self.editing = YES;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Code for heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = nil;
if (self.editing)
cell = (Cell *)[tableView cellForRowAtIndexPath: indexPath];
else
cell = nil;
BOOL expandCell = NO;
NSInteger expandationHeight = 0;
if (cell != nil)
{
for (UIButton *btn in cell.contentView.subviews)
{
NSLog(#"A button was found.");
expandationHeight = 70;
expandCell = YES;
}
}
return expandationHeight + heightOfOtherElements;
}
When I click at a cell nothing happends but everything becomes disabled, I can't click any elements on the hole view. This has something to do with [tableView cellForRowAtIndexPath: indexPath], because if I uncomment that line the UI does not become disabled.
What am I doing wrong?
That's not too complicated though it requires some stuff.
You want to change the content of one cell or of all cells?
To specify a specific height for one cell, use the - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath delegate method.
It is called for each cell each time the table view is displayed. If you want to change a single row, call reloadRowsAtIndexPaths:withRowAnimation: method from UITableView. If you want to update all cells, simply use - (void)reloadData method.
You can also access a specific cell using - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath method from UITableView. Then you can reconfigure it to add various elements on it, as you want.
Thus, when a cell is selected, check whether you have to edit it or not, then :
update your cell get from cellForRowAtIndexPath
be sure your method tableView:heightForRowAtIndexPath: will return the good height
tell the table view to update the view using reloadRowsAtIndexPaths:withRowAnimation: