Ios dequeueReusableCellWithIdentifier中的字幕

Ios dequeueReusableCellWithIdentifier中的字幕,ios,objective-c,uitableview,tableviewcell,Ios,Objective C,Uitableview,Tableviewcell,我有这个密码 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; Song *song = [self.music objectAtIndex:indexPath.row]; cell.textLabel.text = song.title; cell.detailTextLabel.text = song.artist; retu

我有这个密码

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
Song *song = [self.music objectAtIndex:indexPath.row];

cell.textLabel.text = song.title;
cell.detailTextLabel.text = song.artist;

return cell;
我不使用界面生成器。我怎样才能使这个单元格有字幕?我得到的只是一个标准单元格。

试试下面的代码:

UITableViewCell *cell= [self.tableView dequeueReusableCellWithIdentifier:@"Cell"];

if (cell==nil) {
  // Using this way you can set the subtitle 
  cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}

Song *song = [self.music objectAtIndex:indexPath.row];
cell.textLabel.text = song.title;
cell.detailTextLabel.text = song.artist;

return cell;

在CellForRowatineXpath方法中尝试此操作

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

希望有帮助。

有两种方法:

  • 旧式方法是不注册任何类、NIB或单元原型,调用
    dequeueReusableCellWithIdentifier
    ,而不使用
    forIndexXPath

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
        }
    
        Song *song = self.music[indexPath.row];
        cell.textLabel.text = song.title;
        cell.detailTextLabel.text = song.artist;
    
        return cell;
    }
    
    正如我们在其他地方讨论的,这假设您没有为该重用标识符注册一个类

  • 另一种方法是在
    viewDidLoad
    中注册您自己的类:

    [self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"Cell"];
    
    然后使用
    forIndexPath
    选项调用
    dequeueReusableCellWithIdentifier
    ,但会丢失手动测试它是否为
    nil
    (因为它永远不会是
    nil
    ):

    这显然假设您已经实现了一个包含副标题的
    UITableViewCell
    子类(注意,我正在覆盖样式):


  • 就我个人而言,我认为设计一个单元原型(自动注册重用标识符并处理所有其他事情)要容易得多。即使是注册NIB的旧技术也比上述技术简单。但是,如果你想完全以编程的方式完成,这是两种方法。

    这不起作用,我想是因为在方法出列后单元格不是nill。不幸的是,有了这段代码,我得到了空白的TableView。你在Song类中成功地得到了数据吗@是的,我喜欢。我确信我的data getter方法一切正常,因为我将输出记录到Console中,我是否应该使用initWithStyle方法istead of dequeue?RAM的数量很大,我不认为我的几百个单元会对设备造成很大影响。这段代码在没有字幕的情况下仍然生成相同的输出。也许我应该构建一个自定义单元格并使用它?如果声明声明返回nonull,那么dequeueReusableCellWithIdentifier方法如何返回nil?这很有帮助,但我认为此解决方案的内存效率低于dequeue方法。如何使用标识符“Cell”注册tableview?
    [self.tableview registerClass:[UITableViewCell类]forCellReuseIdentifier:@“Cell”];
    您需要创建一个UITableViewCell子类,在init方法中(我认为是initWithStyle或其他),您可以使用subtitle样式初始化super。然后为标识符注册您的子类。我得到了解决方案。谢谢。是的,这是我正在尝试描述的,但现在正在我的手机上。:-)
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
        Song *song = self.music[indexPath.row];
        cell.textLabel.text = song.title;
        cell.detailTextLabel.text = song.artist;
    
        NSLog(@"title=%@; artist=%@", song.title, song.artist); // for diagnostic reasons, make sure both are not nil
    
        return cell;
    }
    
    @interface MyCell : UITableViewCell
    @end
    
    @implementation MyCell
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        return [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    }
    
    @end