我有一个带有自定义原型单元格的UITableViewController。在原型单元内部,我有2个标签和一个图像。这些标记为100,用于图像,101和102标记为标签。我试图访问此方法中的标签
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == 0) {
static NSString *CellIdentifier = @"InfoCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIImageView *albumArtworkImageView = (UIImageView *)[cell viewWithTag:100];
albumArtworkImageView.image = [self getAlbumArtworkWithSize:albumArtworkImageView.frame.size];
UILabel *albumArtistLabel = (UILabel *)[cell viewWithTag:101];
albumArtistLabel.text = [self getAlbumArtist];
UILabel *albumInfoLabel = (UILabel *)[cell viewWithTag:102];
albumInfoLabel.text = [self getAlbumInfo];
return cell;
} else {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
MPMediaQuery *audiobookQuery = [MPMediaQuery audiobooksQuery];
MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue: audiobookTitle forProperty: MPMediaItemPropertyAlbumTitle];
[audiobookQuery addFilterPredicate:albumPredicate];
NSArray *albumTracks = [audiobookQuery items];
NSUInteger trackNumber = [[[albumTracks objectAtIndex:(indexPath.row-1)] valueForProperty:MPMediaItemPropertyAlbumTrackNumber] unsignedIntegerValue];
if (trackNumber) {
cell.textLabel.text = [NSString stringWithFormat:@"%i. %@", trackNumber, [[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyTitle]];
} else {
cell.textLabel.text = [[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyTitle];
}
if ([self sameArtists]) {
cell.detailTextLabel.text = @"";
} else {
if ([[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyArtist]) {
cell.detailTextLabel.text = [[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyArtist];
} else {
cell.detailTextLabel.text = @"";
}
}
return cell;
}
}
继续看故事板
我遇到的问题是我从标签查看视图的行返回nil。我以前做过这种类型的查找,我无法弄清楚为什么他们返回nil。任何帮助将非常感激。我甚至不确定调试的好方法。我是一名C#开发人员,试图学习objective-c / ios编程。谢谢!
答案 0 :(得分:2)
我给你一个替代方案。为UITableViewCell
创建一个自定义类并在那里声明它的视图;然后使用这些属性连接每个单元格的子视图并直接访问它们,而无需调用viewWithTag。
例如,在您的自定义单元格中:
@interface MyCustomCell : UITableViewCell
@property (strong, nonatomic) UIImageView *albumArtworkImageView;
@property (strong, nonatomic) UILabel *albumArtistLabel;
@property (strong, nonatomic) UILabel *albumInfoLabel;
并在您的cellForRowAtIndexPath
方法中:
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.albumArtworkImageView.image = [self getAlbumArtworkWithSize:albumArtworkImageView.frame.size];
cell.albumArtistLabel.text = [self getAlbumArtist];
cell.albumInfoLabel.text = [self getAlbumInfo];
请记住在故事板中的单元格中设置MyCustomCell。
希望这有帮助!