reloadData in a UITableView when deviceOrientation change

走远了吗. 提交于 2019-12-13 13:06:36

问题


I'm working with a UITableView, with UITableViewCell. My appsupports the landscape orientation so i've created 2 UITableViewCell: one for the portraid and one for the landscape.

In my cellForRowAtIndexPath method this is my code

static NSString *CellIdentifier;
static NSString *NibNamed;

UIDeviceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation;

if (deviceOrientation != UIDeviceOrientationLandscapeLeft &&
    deviceOrientation != UIDeviceOrientationLandscapeRight)
{
    CellIdentifier= @"Cell1";
    NibNamed = @"cell_news";
}
else
{
    CellIdentifier= @"Cell2";
    NibNamed = @"cell_news_landscape";
}

[[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:NULL];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
///here i add title, description, ecc... in my UITableViewCell

Then in shouldAutorotateToInterfaceOrientation i wrote this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[my_table reloadData];
return YES;
}

Where is the problem? If i start the app in portrait, when i rotate the device the first time, nothing happen (and the UITableCellView is the portrait_table_cell). When i rotate the device for the second time (and i'm in portrait or in upsideDown), i see the landscape_table_cell (and, of course, i don't see all the text because it goes out from the screen). So.. except the firt time, the other times that i rotate the device, i see the wrong table_cell_view!!

Do you know why? Thanks!


回答1:


Your data is reload before the device changed orientation. shouldAutorotateToInterfaceOrientation is called before any rotation.

Try the didRotateFromInterfaceOrientation

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
 [my_table reloadData];
}

Also note the answer given by Jhaliya, because that solution will be better then reloading you tableview. You should only reload the tableview if the datasource has changed.




回答2:


Not call reloadData from shouldAutorotateToInterfaceOrientation:

Use the UIView autoresizingMask property for both UITableView and UITableViewCell when you create the object for both . because UITableView andUITableViewCellare the subclass ofUIView`.

@property(nonatomic) UIViewAutoresizing autoresizingMask



回答3:


I think there are two problems. 1) shouldAutoRotateToInterfaceOrientation is called BEFORE the view rotates, so if you refresh then, it's too early. 2) I think you need to manage the redraw yourself rather than rely on reloadData. So either override the redraw method of the tableViewCell, or set your autoresizing masks appropriately. Hope this helps. -Mike




回答4:


If the autosizing doesn't suit you well, you could also get the visible cells indexes with

- (NSArray *)indexPathsForVisibleRow

And then get the cells itself with

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

And call a method like

[cell interfaceOrientationDidChangeTo:interfaceOrientation]

And let the job be done by the cell itself.



来源:https://stackoverflow.com/questions/5664890/reloaddata-in-a-uitableview-when-deviceorientation-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!