我正在尝试在cellFotRowAtIndexPath
方法中自定义表格视图单元格,但未应用这些样式。
我将代码放在错误的位置吗?
(void)viewDidLoad {
[super viewDidLoad];
self.buttonTitles = [[NSArray alloc] initWithObjects:@"1 Die", @"2 Dice", @"3 Dice", @"4 Dice", @"5 Dice", @"6 Dice", @"7 Dice", @"8 Dice", @"Roll with a Button", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return buttonTitles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"settingsCell" forIndexPath:indexPath];
// Configure the cell...
UIFont *cellFont = [UIFont fontWithName:@"Raleway-Thin" size:14];
NSString *titles = [self.buttonTitles objectAtIndex:indexPath.row];
cell.textLabel.text = titles;
cell.textLabel.font = cellFont;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.backgroundColor = [UIColor darkGrayColor];
return cell;
}
在视图调试器中有空单元格:
答案 0 :(得分:1)
tableView:numberOfRowsInSection:
中有这一行。
return buttonTitles.count;
这是引用名为buttonTitles的实例变量,而不是viewDidLoad
中指定的属性。将行更改为读取
return self.buttonTitles.count;
您还可以删除buttonTitles
实例变量的声明。你可能不需要它。
答案 1 :(得分:1)
您尚未为tableview设置委托 以下代码将起作用
-(void)viewDidLoad {
[super viewDidLoad];
self.buttonTitles = [[NSArray alloc] initWithObjects:@"1 Die", @"2 Dice", @"3 Dice", @"4 Dice", @"5 Dice", @"6 Dice", @"7 Dice", @"8 Dice", @"Roll with a Button", nil];
UITableView *myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 300, 400)];
myTableView.delegate = self;
myTableView.dataSource = self;
[self.view addSubview:myTableView];
}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return buttonTitles.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"settingsCell"];
if(!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"settingsCell"];
}
// Configure the cell...
UIFont *cellFont = [UIFont fontWithName:@"Raleway-Thin" size:14];
NSString *titles = [self.buttonTitles objectAtIndex:indexPath.row];
cell.textLabel.text = titles;
cell.textLabel.font = cellFont;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.backgroundColor = [UIColor darkGrayColor];
return cell;
}
答案 2 :(得分:0)
从IB中选择单元格和属性检查器,将TableView样式更改为自定义