我正在创建一个问卷调查应用,我需要在其中显示一组问题。我的一个问题类型包含一个网格,如下图所示。任何人都可以提出一个例子或如何实现这一点。
我想创建一个像这样的表格视图
我的代码在Objective-C中。任何帮助将不胜感激。
答案 0 :(得分:0)
没有系统控件可以执行您想要的效果,您必须自定义一个或使用一些开源代码。
这是自定义tableview的示例,用于实现您想要的效果,您可以看看:
这是ViewCtonroller.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property UITableView *tableView;
@end
这是ViewController.m:
#import "ViewController.h"
#import "MyCell.h"
@interface ViewController ()
@property NSString *cellID;
@property int columns;
@property NSMutableArray *dataList;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_cellID = @"MyCell";
_columns = 2;
_dataList = [[NSMutableArray alloc] initWithObjects:@"data0",
@"data1",@"data2",@"data3",@"data4",@"data5",@"data6",@"data7",
@"data8",@"data9",@"data10",nil];
_tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return (_dataList.count / _columns) + 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:_cellID];
if (nil == cell) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_cellID];
}
if (0 == indexPath.row) {
[cell SetData:@"Column0"and:@"Column1"];
}
else{
[cell SetData:_dataList [(indexPath.row-1) * _columns]and:_dataList [(indexPath.row-1) * _columns + 1]];
}
return cell;
}
@end
这是MyCell.h:
#import <UIKit/UIKit.h>
@interface MyCell : UITableViewCell
-(void)SetData:(NSString*)str0 and:(NSString*)str1;
@end
这是MyCell.m:
#import "MyCell.h"
@interface MyCell()
@property UILabel *lb0;
@property UILabel *lb1;
@end
@implementation MyCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
_lb0 = [[UILabel alloc] init];
_lb0.textAlignment = NSTextAlignmentCenter;
[self addSubview:_lb0];
_lb1 = [[UILabel alloc] init];
_lb1.textAlignment = NSTextAlignmentCenter;
[self addSubview:_lb1];
return self;
}
-(void)SetData:(NSString*)str0 and:(NSString*)str1{
_lb0.text = str0;
_lb1.text = str1;
}
-(void)layoutSubviews{
_lb0.frame = CGRectMake(0, 0, self.frame.size.width/2, self.frame.size.height);
_lb1.frame = CGRectMake(self.frame.size.width/2, 0, self.frame.size.width/2, self.frame.size.height);
}
@end
您还可以在GitHub上搜索一些开源项目。
像这样:YHExcelView
希望它可以帮到你。
答案 1 :(得分:0)
您可以使用UICollectionView或UITableView来实现此UI。
我建议你使用UICollectionView,你可以配置每行中有多少个视图,并根据需要自定义每个视图。
对于UITableViewCell,您需要将每一行设计为单个UITableViewCell并加载tableview数据源,这在您的情况下似乎很复杂。