More control over updating UITableView and UITableViewCell - cocoa-touch

I am trying to implement expanding UITableViewCells where I have complete control over the animation of the transition from collapsed to expanded and back.
I have an array, cellDataList, that holds the state of my UITableViewCells, i.e. height, color, data, isExpanded etc.
I implement:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath == selectedIndex)
return expandedHeight;
return collapsedHeight;
}
and I implement:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
some logic that tests if the indexPath is already selected,
which will collapse the cell, if a previous cell needs to be collapsed before the
new expands etc.
*/
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
}
My cell is a custom UITableViewCell where I build my content in drawRect:
The cell has a UIView (background) that serves as a custom background for the cell, this view I would like to
animate from collapsed to expanded and back, not just setFrame in one 'jump', as it does now.
I can't really figure out where to 'intercept' the tableView updater chain, to achieve this.
When I do the [tableView beginUpdates] - [tableView endUpdates] above, it starts to get a bit obscure what happens.
I assume
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
is called to update the cells
at the indexes specified. This in turn will ask heightForCellAtIndexPath which height to set the cell. If I set a breakpoint in my custom cells setFrame: method
I can see the caller is
[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] ().
This sets the new frame for the cell, then I assume [cell setNeedsDisplay] is called somehow so the cell can render for the frame provided.
The [cell setNeedsDisplay] will eventually call my custom drawRect: and render the content of the cell. This works as expected, but I can't figure
out where and how I will animate the frame for my background, also in a way so it is only done when clicked and not every time setFrame: is called (during scrolling etc).
When I do the above the tableView animates to make room for the new cell size, it is
this animation I would like to 'mimic' with the background view on my cell.
I see this effect in apps like Tweetbot and path but I can't seem to replicate it.
Hope some one can help me in the right direction, thanks:)

I am not totally sure that i understood your question exactly:) But you can use Key Value Observer (KVO) to check when you need to play your animation then call your table reload method.
- (void)reloadTable
{
if (self.isViewLoaded) {
[self.tableView reloadData];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[self reloadTable];
}

The best way to do this is to add a dummy cell below the cell that was tapped. I explain this thorougly here.
Basically:
Keep track of what cells are been tapped in your didSelectRowAtIndexPath and update it appropriately. Which one needs to be deleted? Where will the new dummy cell be next?
Return the correct account whenever the dummy cell is present.
Account for the fact that the dummy cell is not part of your model. Offset the indexPath.

Related

How reorder row in customize UITableViewCell

So far I have customized my UITableViewCell with the icon reorder row.
I want my cell can be reordered when long press on the reorder icon and then move it.
The image below is what I have expected.
I have read many document they provided only long press on cell and move it but I don't want it because long press on cell I have my another gesture is renaming it; and also I don't want to use the edit/done of UITableView default from OS; so I decided to put reorder icon but I can not do, because I'm new in objective c.
Can anyone help by providing sample code here?
Thank for reading.
Override two methods of UITableViewDataSource
For reordering to work you will first need to bring the Table in editing mode. Which you can either do it from viewDidLoad or from a IBAction method.
-(void) viewDidLoad{
[super viewDidLoad];
self.myTableView.editing = YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
//Manipulate your data array.
}
By Default editing style is UITableViewCellEditingStyleDelete. So if you don't override the below method it will show both rearrange as well delete icon.
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
Since, you have long gesture of your cell causes issue with the long gesture of the re-ordering of cell. You can add a UIView to the cell. Add all your elements in this UIView. Now, attach a UILongPress Gesture to this UIView (not to the cell.).

UITableView Center Cell Details

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.

UITableViewCell unselectable while its accessoryView is selectable

I want to have a UITableViewCell that is not selectable while its accessory view (a UISwitch in my case) is editable.
The issue is that I have two other cells of which one needs to remain active; this is very similar to the following image of the Time/Date selector from the iOS calendar app:
http://www.theiphoneblog.com/images/stories/2009/01/photo.jpg
I cannot post the image due to being a new user.
Note that in this view the "All-day" cell cannot be selected but its UISwitch can be changed, while one of "Starts" and "Ends" cells must remain selected.
I've tried both:
[cell setUserInteractionEnabled:NO];
[cell.contentView setUserInteractionEnabled:NO];
The first one works but does not allow the switch to be changed whereas the second does not work, it allows one of the top cells to be deselected which I do not want to happen.
Implement in delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([selectablePaths contains:indexPath])
{
// cell selected
selectedPath = indexPath;
}else
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[tableView selectRowAtIndexPath:selectedPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}
And enable user interaction for all cells.

Add editing elements to UITableViewCell

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:

How to unselect a UITableViewCell after you have done some action using didSelectRowAtIndexPath?

Currently I do some work inside the didSelectRowAtIndexPath delegate and noticed that my row is still "selected" after I come back to the view controller later.
How can I unselect this row inside the delegate itself?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//do logic and ... it remains selected in the view?
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Typically, you want to deselect the row when the view controller appears again:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
That's what UITableViewController does implicitly, and animating the deselection process after the view controller appears again is a helpful hint to the user as to which cell they selected previously.