我正在创建一个带有4个单元格的表观视图。并且必须根据设备的方向将单元格分为各种尺寸。
第一列和第二列将具有相同的宽度,而第三列将是其中最大的宽度,第四列的宽度将比第三列小,但不小于第一个也不是第一个。
。 。有没有动态的方法?
这是模板
我在计算宽度方面遇到困难,这是我到目前为止所拥有的。我还没有创建L2,L3和L4,因为我很难计算它的宽度
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells" forIndexPath:indexPath];
UIInterfaceOrientation newOrientation = [UIApplication sharedApplication].statusBarOrientation;
UILabel *typeLabel = [[UILabel alloc] init];
if (cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cells"];
}
if(newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight)
{
l1 = [[UILabel alloc] initWithFrame:CGRectMake((self.tableView.bounds.size.width/15+20), 50, self.tableView.bounds.size.width/12, 21)];
}
else
{
l1 = [[UILabel alloc] initWithFrame:CGRectMake((self.tableView.bounds.size.width/15+50), 50, self.tableView.bounds.size.width/12, 21)];
}
[cell.contentView l1];
[cell.contentView l2];
[cell.contentView l3];
[cell.contentView l4];
return cell;
}
创建一个名为" customcell"的自定义表视图单元格。您可以使用带有XIB选项的新文件来创建它。
customcell.h
@interface CustomCell : UITableViewCell
@end
customcell.m
@implementation MessagesTableViewCell
//perform your custom implementation here.
@end
customcell.xib 使用Auto-Layout调整用户界面中单元格中的标签。您可以在需要的前2个标签上添加4个标签,并具有恒定宽度,最后一个标签具有所需宽度,并让中心(第三个)标签在第二个标签和第4个标签上延伸。它将帮助您以更轻松的方式管理UI。Auto-Layout是一项功能,有助于为不同尺寸构建内容。您可以在https://www.sitepoint.com/self-sizing-cells-uitableview-auto-layout/
上使用自动布局找到动态表视图单元格在您的tableview类中使用它
static NSString *cellIdentifier = @"CustomCell";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
我希望这对您有帮助。
使用程序化计算和方向更新的代码实现
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/////////////////////////Programatic Implementation///////////////////////////////////
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell==nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
//orientation is Portrait
long widthForL1L2 = tableView.frame.size.width * .10;
long widthForL3 = tableView.frame.size.width * .60;
long widthForL4 = tableView.frame.size.width * .20;
UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, widthForL1L2, cell.frame.size.height)];
l1.backgroundColor = [UIColor redColor];
[cell addSubview:l1];
UILabel *l2 = [[UILabel alloc] initWithFrame:CGRectMake(widthForL1L2, 0, widthForL1L2, cell.frame.size.height)];
l2.backgroundColor = [UIColor yellowColor];
[cell addSubview:l2];
UILabel *l3 = [[UILabel alloc] initWithFrame:CGRectMake(l2.frame.origin.x+widthForL1L2, 0, widthForL3, cell.frame.size.height)];
l3.backgroundColor = [UIColor greenColor];
[cell addSubview:l3];
UILabel *l4 = [[UILabel alloc]initWithFrame:CGRectMake(l3.frame.origin.x+widthForL3, 0, widthForL4,cell.frame.size.height)];
l4.backgroundColor = [UIColor blueColor];
[cell addSubview:l4];
}else{
//orientation is Landscape
//perform the same calculation as above or modify it according to your needs in landscape orientation.
}
return cell;
}
不要忘记包括这个定向更改的功能。当电话方向更改时,此功能调用表重新加载。
//Available iOS version >=8.0
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
//Reload the tableview on orientation change, to match the new width of the table.
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self.tableView reloadData];
} completion:nil];
}