【发布时间】:2014-05-20 21:06:30
【问题描述】:
我有一个类ViewController,我用代码而不是nib文件在其中创建TableView,并且我想要具有nib文件的UITableViewCell的自定义单元格。
我使用宽度为 320px 的代码创建 TableView,我想使用 UITableViewCell 创建自定义单元格,我的单元格宽度为 300px,并放入我的 TableView 中。
注意:我希望这些单元格放在我的表格中心,并且这些单元格距离两侧有 10px 的距离!!!
这是我的代码:
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
table = [self makeTableView];
[self.view addSubview:table];
[self.view setBackgroundColor: RGB(193,60,46)]; //will give a UIColor objct
name = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
// Do any additional setup after loading the view from its nib.
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
-(UITableView *)makeTableView
{
CGFloat x = 0;
CGFloat y = 0;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
CGRect tableFrame = CGRectMake(x, y, width, height);
UITableView *tableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStyleGrouped];
tableView.rowHeight = 60;
tableView.sectionFooterHeight = 22;
tableView.sectionHeaderHeight = 22;
tableView.scrollEnabled = YES;
tableView.showsVerticalScrollIndicator = YES;
tableView.userInteractionEnabled = YES;
tableView.bounces = YES;
tableView.backgroundColor = [UIColor clearColor];
tableView.delegate = self;
tableView.dataSource = self;
return tableView;
}
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *)[self.table dequeueReusableCellWithIdentifier:@"myCell"];
if (cell == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for (id currentObject in topLevelObject) {
if ([currentObject isKindOfClass:[CustomCell class]]) {
cell = (CustomCell *)currentObject;
break;
}
}
}
// Configure the cell...
cell.titleLable.text = [name objectAtIndex:indexPath.row];
return cell;
}
这是我的代码:TableCode
【问题讨论】:
-
一种方法是保持自定义单元格宽度不变并开始对齐单元格内容以从 10px 开始。
-
在您的自定义 nib 文件中,保持单元格的宽度与 UIViewController 相同。根据需要放置单元格的所有内容,例如开始将 UILabel 放置在 x = 10px、y = 0px 和所需的宽度和高度。
-
我好困惑!!!!请告诉我如何在 UITableViewCell 中添加 UIView
标签: ios objective-c uitableview width