【问题标题】:How to customizing a table view cell如何自定义表格视图单元格
【发布时间】:2015-11-03 18:37:59
【问题描述】:

我正在尝试在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;
}

更新:

在视图调试器中有空单元格:

【问题讨论】:

  • 视图控制器是 UITableViewController 还是 UIViewController 的子类?
  • @Avi,它是UITableViewController的子类。
  • 尝试将您的故事板上的单元格类型更改为“自定义”。不知道是否可以更改“基本”单元格类型,或其他预定义样式、属性。
  • 这就是一个空的 tableView 的样子。在tableView:numberOfRowsInSection: 中设置断点,或者添加日志,查看返回的行数。

标签: objective-c uitableview custom-cell


【解决方案1】:

您在tableView:numberOfRowsInSection: 中有此行​​。

return buttonTitles.count;

这是引用一个名为 buttonTitles 的实例变量,而不是您在 viewDidLoad 中分配的属性。换行阅读

return self.buttonTitles.count;

您还可以删除buttonTitles 实例变量的声明。你可能不需要它。

【讨论】:

  • 是否显示了任何单元格?如果您检查视图调试器,您会看到单元格吗?从您的屏幕截图中很难判断 tableView 是否完全可见。
  • 也许你错过了代表。 self.tableview.delegate=self;self.tableView.dataSource = self;
  • @gomezluisj,这段代码会去吗? cellForRowAtIndexPath?
  • 没有显示任何单元格。只是一个空白的表格视图控制器。
  • @CalebKleveter 在viewDidLoad中添加代码,不要忘记使用你设置的tableview名称。
【解决方案2】:

您还没有为 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;
}

【讨论】:

  • myTableView.delegate = self; myTableView.dataSource = 自我;这两行是调用委托的主要内容。但是要调用委托,您必须声明 委托
  • 目前还没有。我在.m 文件的@interface 中添加了代表。那是对的吗? @interface SettingsTableViewController () <UITableViewDataSource, UITableViewDelegate>
  • 您可以将其添加到 .h 文件或 .m 文件中的任何位置。现在可以使用了吗?
  • 不,看起来一样。
  • 你是否在其他地方分配了这个视图控制器?您是否尝试过在此视图控制器中的 viewDidLoad 中保留断点并且控件是否出现?
【解决方案3】:

从 IB 中选择单元格并在属性检查器中,将 TableView 样式更改为自定义

【讨论】: