UITableView correcting scroll position after device rotation

后端 未结 6 713
予麋鹿
予麋鹿 2021-01-03 13:45

I\'m building something like a reader for a book. When the user rotates the phone I want to increase the font size. I\'m using a UITableView to display chunks o

6条回答
  •  鱼传尺愫
    2021-01-03 14:06

      - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)
    fromInterfaceOrientation 
    {
    NSLog(@"didRotateFromInterfaceOrientation:%d",fromInterfaceOrientation);
    [tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
    }
    

    Then I would recommend adding either the interfaceOrientation number or simply the table width to the dequeue cell name that way the tableView knows that cells in one rotation are different from those in another. Like so:

    - (UITableViewCell *)tableView:(UITableView *)tv
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
         withType:(NSString *)s_type
    {
    
    UITableViewCell *cell = nil;
    // add width of table to the name so that rotations will change the cell dequeue names
    s_cell = [s_cell stringByAppendingString:
              [NSString stringWithFormat:@"%@%d",@"Width",(int)tv.bounds.size.width]
              ];
    
    NSLog(@"%@",s_cell);
    cell = [tv dequeueReusableCellWithIdentifier:s_cell];
    if( cell == nil ) { 
        cell = [[[UITableViewCell alloc]
                 initWithFrame:CGRectZero reuseIdentifier:s_cell] autorelease];
        }
    }
    

提交回复
热议问题