dequeueReusableCellWithIdentifier 中的副标题
Subtitle in dequeueReusableCellWithIdentifier
我有这个代码
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;
在你的 cellForRowAtIndexPath 方法中试试这个
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
希望对您有所帮助。
有两种方法:
老办法是不注册任何class,NIB或cell原型,调用dequeueReusableCellWithIdentifier
而不用forIndexPath
:
- (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;
}
正如我们在别处讨论的那样,这假设您没有为该重用标识符注册 class。
替代方法是在 viewDidLoad
:
中注册您自己的 class
[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"Cell"];
然后调用 dequeueReusableCellWithIdentifier
with forIndexPath
选项,但丢失了手动测试是否为 nil
的代码(因为它永远不会将是 nil
):
- (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;
}
这显然假设您已经实现了一个包含副标题的 UITableViewCell
subclass(注意我正在覆盖样式):
@interface MyCell : UITableViewCell
@end
@implementation MyCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
return [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
@end
就我个人而言,我认为设计一个单元原型(自动注册重用标识符并处理所有这些其他事情)要容易得多。即使是注册 NIB 的旧技术也比上面的更容易。但是,如果您想完全以编程方式完成,这就是这两种方法。
我有这个代码
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;
在你的 cellForRowAtIndexPath 方法中试试这个
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
希望对您有所帮助。
有两种方法:
老办法是不注册任何class,NIB或cell原型,调用
dequeueReusableCellWithIdentifier
而不用forIndexPath
:- (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; }
正如我们在别处讨论的那样,这假设您没有为该重用标识符注册 class。
替代方法是在
中注册您自己的 classviewDidLoad
:[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"Cell"];
然后调用
dequeueReusableCellWithIdentifier
withforIndexPath
选项,但丢失了手动测试是否为nil
的代码(因为它永远不会将是nil
):- (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; }
这显然假设您已经实现了一个包含副标题的
UITableViewCell
subclass(注意我正在覆盖样式):@interface MyCell : UITableViewCell @end @implementation MyCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { return [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; } @end
就我个人而言,我认为设计一个单元原型(自动注册重用标识符并处理所有这些其他事情)要容易得多。即使是注册 NIB 的旧技术也比上面的更容易。但是,如果您想完全以编程方式完成,这就是这两种方法。