Here's a video of what's going on: https://imgflip.com/gif/kgvcq
Basically, if the cells scroll past the bottom edge of the screen, it won't bounce back. I've tried updating the contentSize of the tableView but that doesn't seem to be the issue. I've also made sure to declare the rowHeight and still no luck. Lastly, I've made sure the bounce properties of the tableView are set properly.
Sorry for not putting up code, here it is:
// data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"frame height: %f", tableView.frame.size.height);
NSLog(#"content size height: %f", tableView.contentSize.height);
static NSString *CellIdentifier = #"HabitCell";
HabitTableViewCell *cell = (HabitTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.viewController = self;
cell.delegate = self;
// edit cell
return cell;
}
The NSLogs are returning: 568 and 400 respectively. Would it be the frame causing problems? Also, I have not overridden scrollViewDidScroll.
Implemented Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.habits count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:_expandIndexPath]) {
return 450 + heightToAdd;
}
return 100;
}
Fixed: I had a call to scrollToRowAtIndexPath in my UIPanGestureRecognizer method. Removed it and it now works perfectly.
Related
I am calculating height of Cell programatically in heightForRowAtIndexPath-
My complete method is:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
if([cell isKindOfClass:[Custom class]])
{
return 100;
}
else
{
return 20;
}
}
My app takes 3-5 sec to Show the ViewController. Although cellForRowAtIndexPath is called and I can show the logs in Console.
But My ViewController not loaded.
When just return the height using this code app works fine:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
I don't know that is the issue with this line:
id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
What I am missing in this issue?
You have some logic in the cellForRowAtIndexPath: method that determines what type of cell that row should be. Add that logic into the heightForRowAtIndexPath: method to determine the what the height of each row should be.
Your this line is wrong
id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
it will force your cell to reuse.
Use
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell=[tableView cellForRowAtIndexPath:indexPath];
if([cell isKindOfClass:[CustomCellClass class]])
{
return 100;
}
else
{
return 20;
}
}
I am unsure if I'm setting up the UITableView the right way when using multiple custom cell views. I seem to have my table working however, i think the reuse of cells makes the first cells you see once you load the view the same size as the very first cell which should be the only cell thats double in size. Once I scroll passed them and back up they go to the size thats set for them.
Is there a right way of doing this? I've looked through other forms and tried their ways but this is the closed I've gotten it to work with just that one flaw.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"profileCell";
static NSString *CellIdentifier2 = #"profileCells";
if (indexPath.section == 0) {
PFProfileTableCellView * cell = (PFProfileTableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
//only one row in this section
return cell;
} else{
PFWallPostTableViewCell * cell = (PFWallPostTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
return cell;
}
}
Use this UITableView delegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; and return row height according to the sections of your tableview like the example below
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return 100.0;
}
return 50.0;
}
my platform is ios8 and xcode 6.3.1
tableview's delegate like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
so, the delegate of heightForRowAtIndexPath: should be execute three times , but my code execute four, why ?
My code :
init tableView
- (void)setupTableView {
_selectTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_selectTableView.delegate = self;
_selectTableView.dataSource = self;
_selectTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:_selectTableView];
}
other delegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger section = indexPath.section;
static NSString *identified = #"selectCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identified];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identified];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return [self cellWith:cell andSection:section];
}
- (UITableViewCell *)cellWith:(UITableViewCell *)cell andSection:(NSInteger)section {
....
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
CGFloat height = 0;
if (section != SVCellTypeHot) {
height = 5;
}
return height;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 5)];
[footerView setBackgroundColor:[UIColor lightGrayColor]];
return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 0;
switch (indexPath.section) {
case SVCellTypeBanner:
{
height = kHeaderViewHeigth;
}
break;
case SVCellTypeRecommand:
{
height = kRecommandViewHeight;
}
break;
case SVCellTypeHot:
{
height = kHotViewHeight;
}
break;
default:
break;
}
return height;
}
heightForRowAtIndexPath allows the delegate to specify rows with varying heights. If this method is implemented, the value it returns overrides the value specified for the rowHeight property of UITableView for the given row. There is no guarentee that this method can only be called 'section count * item count' times in the UITableView. As you can tell from its name, it will calculate the height for the cells at IndexPath, so combining the re-using technique, this method will be called many times, as long as it needs to calculate the height for cell at IndexPath
So actually, it is a system behaviour to decide how many times it should be called and when. In your comment, it seems like something changed in indexPath {1-0} so heightForRowAtIndexPath is called twice for {1-0}. You might need to check have you changed any content that cause iOS to re-calculate the cell's height.
Without knowing more details, this is the best we can do to provide you some clues to debug. However, you should not rely on how many times it calls heightForRowAtIndexPath, again, this can be called at any time, as long as you scroll or change any frame inside that cell
heightForRowAtIndexPath: will execute as many times as it needs to. If you are scrolling, for example, it will execute as offscreen cells are about to come onscreen. That method should always be able to provide the correct height and you normally shouldn't be concerned with how often it's called. cellForRowAtIndexPath: executes 3 times as it should.
Here's what I'm trying to do.
I have a UITableViewCell lets say with fixed height of 300 (it is actually a variable size height but I'm trying to simplify the example)
What I want to achieve is that when I scroll back up - I will have a "thumbnailed" version of the cell - with height of 75
I managed to make it happen, but now the problem is that when I scroll up the previous cell heights are adjusted and the scroll position "jumps" once the cell sizes are smaller, which causes the view to "jump back down" when he scrolls up.
How can I adjust it?
The code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (indexPath.row < lastViewedChapter)
{
cell = [self generateChapterCell:tableView indexPath:indexPath collapsed:YES];
}
else
{
cell = [self generateChapterCell:tableView indexPath:indexPath collapsed:NO];
if (indexPath.row > lastViewedChapter)
{
lastViewedChapter = indexPath.row;
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < lastViewedChapter)
{
return 73;
}
else
{
return 300; //actually here is a code that calculates the height
}
}
You've reduced height of the upper cell and then other cells moved up to fill that space while you were still scrolling right?
Try to set new tableView.contentOffset when you change the cell's height.
In your case the contentOffset.y should be (old contentOffset.y - (300 - 73)) when you return the cell's height as 73.
I didn't test on this but I think it may help and you must calculate new contentOffset for other case too (when scroll down, when table reload data).
static NSInteger _lastRow = -1;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (_lastRow == -1) {
_lastRow = indexPath.row;
return 300;
} else {
if (_lastRow > indexPath.row) {
_lastRow = indexPath.row;
if ([tableView rectForRowAtIndexPath:indexPath].size.height == 300) {
[tableView setContentOffset:CGPointMake(tableView.contentOffset.x, (tableView.contentOffset.y - (300 - 73)))];
}
return 73;
} else {
_lastRow = indexPath.row;
return 300;
}
}
}
This code work fine but still has some bugs (the first row height when first load data is like you have scroll up to it once, when you scroll up to top fast it bounced not normally) but I hope this should help you.
This is something that will definitely happen since you have changed cell heights.
The question is how to mitigate this kind of bad user experience.
UITableView are subclassed from UIScrollView. UIScrollViews provide delegate which is available in UITableView class as well.
Do the following.
self.tableView.delegate = self;
And then implement the following function. In the following, location is a CGPoint variable defined in your header.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
location = tableView.contentOffset;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
CGPoint newLocation = tableView.contentOffset;
if (CGPointEqualToPoint(location, newLocation))
{
NSLog(#"are equal");
tableView.contentOffset = CGPointMake(location.x, location.y-227);
}
}
How do you hide a static cell?
I would like to hide and static cell if an image does not exist.
I tried:
imageCell.hidden = YES; //did not work
I have seen answers suggesting to change datasource or use:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;// will hide all cell
}
But I couldnt find a way to do this with a specific view cell.
what I want to achieve:
if(image==nil){
//hide imageCell
}
Now here is the catch , the image is downloaded asynchronously, so deleguate methods might be called before the attempted downlaod.
Do the following :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 2 && !myImageIsLoaded)
return 0; // Will hide just the third row of your table if myImageIsLoaded is false
return 44;
}
And you can use the following to animate all whenever you want (e.g. each time an image as loaded) :
[myTable beginUpdate];
[myTable endUpdate];
If your cells are static so it should work. Otherwise you could encounter some problems.
If you want literally hide the cell that image does not exist, you can try this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
return [cell imageView] ? 44.0f : 0.0f;
}