我使用TableViews
中的多个iOS
创建多列表格。如附图所示,TableViews
并排放置。由于TableView具有TapGesture的所有功能,例如每个单元格,滚动等,我喜欢我实现的方式。
唯一的问题是滚动。由于每个TableView都有自己的滚动,因此我无法在彼此之间同步滚动。如果不是,则不同列中每行的所有数据都不匹配。
如何在TableView之间同步滚动,即如果我滚动一个TableView,所有TableView都应滚动在一起。
感谢。
答案 0 :(得分:1)
有一些方法可以让它发挥作用。但在我看来,问题的解决方法是错误的。对于这个问题,我会使用UICollectionView
。这样,您就拥有dataSource
一个UITableView
,而且比{{1}}更具灵活性。您也不需要使用任何 hacks 来进行滚动同步。
答案 1 :(得分:0)
将您的UITableView划分为您要在示例代码中显示的列数,然后添加自定义标签并为其提供标记
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *ci=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ci];
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ci];
}
if (tableView == your tablename)
{
CGFloat widthLabelHeader = CGRectGetWidth(tablename.frame)/7; // 7 is the number of columns in table view
serialnmbrlbl = (UILabel*)[cell.contentView viewWithTag:401];
if(!serialnmbrlbl)
{
serialnmbrlbl = [[UILabel alloc]init];
serialnmbrlbl.frame = CGRectMake(0, 0, widthLabelHeader, 50);
serialnmbrlbl.layer.borderWidth = 0.5;
serialnmbrlbl.tag = 401;
serialnmbrlbl.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
[cell.contentView addSubview:serialnmbrlbl];
}
serialnmbrlbl.textAlignment = NSTextAlignmentCenter;
diagnosisnamelbl = (UILabel*)[cell.contentView viewWithTag:402];
if(!diagnosisnamelbl)
{
diagnosisnamelbl = [[UILabel alloc]init];
diagnosisnamelbl.frame = CGRectMake(0, 0, widthLabelHeader, 50);
diagnosisnamelbl.layer.borderWidth = 0.5;
diagnosisnamelbl.tag = 402;
diagnosisnamelbl.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
[cell.contentView addSubview:diagnosisnamelbl];
}
diagnosisnamelbl.textAlignment = NSTextAlignmentCenter;
dischargeamtlbl = (UILabel*)[cell.contentView viewWithTag:403];
if(!dischargeamtlbl)
{
dischargeamtlbl = [[UILabel alloc]init];
dischargeamtlbl.frame = CGRectMake(0, 0, widthLabelHeader, 50);
dischargeamtlbl.layer.borderWidth = 0.5;
dischargeamtlbl.tag = 403;
dischargeamtlbl.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
[cell.contentView addSubview:dischargeamtlbl];
}
dischargeamtlbl.textAlignment = NSTextAlignmentCenter;
然后应用第二个委托方法
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(tableView == hospitaltable)
{
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:
CGRectMake(0, 0, hospitaltable.frame.size.width, 45.0)];
sectionHeaderView.backgroundColor = [UIColor colorWithRed:(83/255.f) green:(200/255.f) blue:(36/255.f) alpha:1];
CGFloat widthLabelHeader = CGRectGetWidth(hospitaltable.frame)/7;
UILabel *namelabel1 = (UILabel*)[sectionHeaderView viewWithTag:201];
if(!namelabel1)
{
namelabel1 = [[UILabel alloc]init];
namelabel1.frame = CGRectMake(0, 0, widthLabelHeader, 45);
namelabel1.layer.borderWidth = 0.5;
namelabel1.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
[sectionHeaderView addSubview:namelabel1];
}
namelabel1.text = @"Sr. NO.";
namelabel1.textAlignment = NSTextAlignmentCenter;
namelabel1.lineBreakMode = NSLineBreakByTruncatingMiddle;
namelabel1.numberOfLines = 2;
namelabel1.backgroundColor = [UIColor colorWithRed:(83/255.f) green:(200/255.f) blue:(36/255.f) alpha:1];
UILabel *orderdateLbl = (UILabel*)[sectionHeaderView viewWithTag:202];
if (!orderdateLbl) {
orderdateLbl = [[UILabel alloc]init];
orderdateLbl.frame = CGRectMake(CGRectGetMaxX(namelabel1.frame), 0, widthLabelHeader, 45);
orderdateLbl.layer.borderWidth = 0.5;
orderdateLbl.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
[sectionHeaderView addSubview:orderdateLbl];
}
orderdateLbl.text = @"DIAGNOSIS NAME";
orderdateLbl.textAlignment = NSTextAlignmentCenter;
orderdateLbl.lineBreakMode = NSLineBreakByTruncatingMiddle;
orderdateLbl.numberOfLines = 2;
orderdateLbl.backgroundColor = [UIColor colorWithRed:(83/255.f) green:(200/255.f) blue:(36/255.f) alpha:1];
也应用此委托方法..
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 45;
}
最后
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45;
}
借助此代码,您将获得所需的tableView