On iOS 9, to set up my tableView with UITableViewAutomaticDimension and a 200.f estimatedRowHeight on viewDidLoad.
[self.tableView setEstimatedRowHeight:200.f];
[self.tableView setRowHeight:UITableViewAutomaticDimension];
It works well for cells with images, buttons or labels but for a cell with a textView and button, the button appears correctly but the textView doesn't expand when once my tableView updated.
[self.tableView beginUpdates];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
Do you have any ideas about want could be my mistake?
Should I update the heigth constraint programmatically in my cell?
Thanks
I found the solution, my UITextView was set as scrollable, I unchecked it in IB and now it works.
Related
I have UITableViewCell with UICollectionView inside. All constraints are setted up. I have a dataSource property in my cell. Setter on my dataSource
- (void)setDataSource:(id<MyCellDataSource>)dataSource
{
self->_dataSource = dataSource;
[self.collectionView reloadData];
[self updateTagsWrapperConstraint];
}
- (void)updateTagsWrapperConstraint
{
CGFloat height = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
self.tagsWrapperHeightConstraint.constant = height;
}
Where self.tagsWrapperHeightConstraint is my wrapper UIView height constraint. The problem is that I have to use double tableView data reloading inside my API fetching method.
[self.tableView reloadData];
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
[self.tableView reloadData];
If I don't implement this - my cell will update her height after another data fetching.
P.S I have a second controller where this works up correctly. Help me out. Thanks in advance, Artem. Sorry for my poor english.
I'm starting a new project for tvOS. I have created a programmatically UITableView inside my viewcontroller. Inside the UITableView i have created 5 cells. Until now everything works, but the problem happens when i'm trying to programmatically scroll to cell 4 so i'm doing:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3
inSection:0];
[tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
But the code above does nothing. It always start at first cell and highlight it as the selected one. I want to be able to programmatically start at another cell and it most be highlight so when i use the arrow keys down or up it scroll from there and not first cell. Please help me in doing that
You should do next:
In viewController that respond for UITableView, implement next function:
- (UIView *)preferredFocusedView {
return self.tableView;
}
where tableView is your table, and after that, override next function
- (NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView {
return [NSIndexPath indexPathForRow:_exerciseIndex inSection:0];
}
where _exerciseIndex index that you need to focus.
In one of the views there is a UITableView which is getting updated rather often.
Tracking the changes are done in a classic way using "reloadRowsAtIndexPaths"
-(void)refreshCells:(NSArray *)changedCells
{
NSLog(#"refreshCells %i",[changedCells count]);
[TableView beginUpdates];
[TableView reloadRowsAtIndexPaths:changedCells withRowAnimation:UITableViewRowAnimationBottom];
[TableView endUpdates];
}
Question: How can I preserve the user's last selected Cell. the cell position may change after each update refreshCells?
You can save the current selection with
NSIndexPath *selectedRow = [self.tableView indexPathForSelectedRow];
before the reload and select it again with
if (selectedRow) {
[self.tableView selectRowAtIndexPath:selectedRow animated:NO scrollPosition:UITableViewScrollPositionNone];
}
after the reload. The cell position does not change unless you call insertRowsAtIndexPaths: or deleteRowsAtIndexPaths:.
I'm having a little problem:
i made my setEditing method:
- (void)setEditing:(BOOL)editing animated:(BOOL)animate {
[super setEditing:editing animated:animate];
[mainTableView reloadData];
}
i'm using the reloadData to call: cellForRowAtIndexPath, and then, if the table is in edit mode, i'll change the appearance of my cell (hiding some labels, for example);
The problem is when i call [mainTableView reloadData] the Edit animation (the red circle slides from left to right and my cell slides to the right) doesn't exist. If don't call it, everything works ok, but i can't customize my cell, since cellForRowAtIndexPath is not called again.
Any suggestion to make it work ??
Thanks!
Maybe you will try to update your table with [tableView beginUpdates] and [tableView endUpdates]? Do you need to reload all the cells or only some of them?
EDIT: Here's the code for reloading all the cells:
[tableView beginUpdates];
NSMutableArray *updatedPaths = [NSMutableArray array];
for (NSNumber *row in yourArray) {
NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:[row intValue] inSection:0];
[updatedPaths addObject:updatedPath];
}
[tableView reloadRowsAtIndexPaths:updatedPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
yourArray is NSArray instance where you store your cell.textLabel values or something like that...
I have a grouped table view with textfields in the tableview. For the keyboard to not hide the textfields, in the textFieldShouldBeginEditing, I am calling the scrollToRowAtIndexPath method and setting the scroll position of the row being edited to the top of the tableview.
The scrolling does happen, but as soon as the keyboard appears (ie as soon as the textFieldShouldBeginEditing returns YES), the table scrolls back to its original position and displays the first row of the first section on top. I am not calling reloadTable after making a call to scrollToRowAtIndexPath.
The problem occurs only for row 4 and 5 (bdate and zip) password2 works as expected.
This is the code I am using to scroll to a particular row
if(textField == password2){
indPath = [NSIndexPath indexPathForRow:3 inSection:0];
[self.tableView scrollToRowAtIndexPath:indPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
return YES;
}
else if(textField == bdate){
indPath = [NSIndexPath indexPathForRow:4 inSection:0];
[self.tableView scrollToRowAtIndexPath:indPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
return YES;
}
else if(textField == zip){
indPath = [NSIndexPath indexPathForRow:5 inSection:0];
[self.tableView scrollToRowAtIndexPath:indPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
return YES;
}
Can someone please tell me what could be going wrong? Any insight would help.
Thanks
I suspect what is going on is that the table view is rubber banding back, just like a webview in Safari does when you run pass its bounds. The most likely reason for this is that when you pop up the keyboard it is on top of the table view, but the table view has not physically contracted, thus all the UITableViewCells fit within the partially obscured UITableView, and when some of them scroll off it rubber bands so they all are within it.
In order to test this try reducing the size of the list view so that you can see the whole thing when the keyboard is displayed, if the bug goes away you want to write code that dynamically changes the size as the keyboard animates in and out.
//For iOS 7
[self.TableView reloadData];
NSIndexPath *indexPat = [NSIndexPath indexPathForRow:1 inSection:1];
[self.TableView scrollToRowAtIndexPath:indexPat atScrollPosition:UITableViewScrollPositionTop animated:YES];