获取屏幕转向和根据转向方向设置宽度;
1、获取屏幕转向事件:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotateScreen) name:UIDeviceOrientationDidChangeNotification object:nil];
[NSNotificationCenter defaultCenter]:事件中心对象;
addObserver:self :添加到哪个对象;
selector:@selector(rotateScreen): 事件触发后执行什么方法;
name:UIDeviceOrientationDidChangeNotification: 设备方向改变事件;
[UIDevice currentDevice].orientation == 1 home键是否在下;
[UIDevice currentDevice].orientation 有7个方向(枚举类型),0,unknown,1,2是home键再上或再下,3,4其余为左右;
为上下设置tableView的宽度为屏幕的width,为左右时设置屏幕的宽度为height; 5、屏幕向上。。
[UIDevice currentDevice].orientation 表示device 设备的方向,设备!!!
[UIApplication sharedApplication].statusBarOrientation 表示application程序的方向,只有4个枚举值,分别对应,device中的1,2,3,4没有其它
uidevie表示当前屏幕的方向,即时转动的方向,而uiApplication表示的是屏幕从哪个方向转成现在的。
-(void)rotateScreen{
NSLog(@"interfaceOrientation %d",[UIDevice currentDevice].orientation);
if([UIDevice currentDevice].orientation != 5 && [UIDevice currentDevice].orientation !=2){
[self setKColumn];
[self.tableView reloadData];
}
}
根据屏幕方向设置tableView宽度和tableView中每个row中button大小;
-(void)setKColumn{
if(isIPad){ //判断是否为iPad
if([UIDevice currentDevice].orientation == 1 ){
self.kColumn = 3;
self.screenWidth = [[UIScreen mainScreen] bounds].size.width;
}else{
self.kColumn = 4;
self.screenWidth = [[UIScreen mainScreen] bounds].size.height;
}
}else{
if([UIDevice currentDevice].orientation == 1 ){
self.kColumn = 2;
self.screenWidth = [[UIScreen mainScreen] bounds].size.width;
}else{
self.kColumn = 3;
self.screenWidth = [[UIScreen mainScreen] bounds].size.height;
}
}
}
=========================
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identity = @"cell";
DLTableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identity];
if(cell == nil){
cell = [[DLTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identity width:self.screenWidth kCloumn:self.kColumn];
NSLog(@"resource cell %f",self.screenWidth);
}
//cell.textLabel.text = ((Product *)[self.array objectAtIndex:indexPath.row]).name;
int location = indexPath.row *self.kColumn;
int length = self.kColumn;
if(location + length >= self.array.count){
length = self.array.count - location;
}
NSRange range = NSMakeRange(location, length);
NSArray *rowProduct = [self.array subarrayWithRange:range];
// 设置这个行Cell所需的产品数据
[cell setRowProducts:rowProduct];
return cell;
}