我有一个自定义原型单元格的UITableViewController。在原型单元格内有两个标签和一个图像。这些标签用100表示图像,101和102用于标签。我试图访问标签这种方法viewWithTag返回零在cellForRowAtIndexPath方法
- (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;
}
}
继承人里面看看故事板
这个问题我有是我仰望的观点从标签的线返回零。我之前做过这种查找,但我无法弄清楚为什么他们返回零。任何帮助将非常感激。我甚至不确定调试的好方法。我是一名试图学习Objective-C/ios编程的C#开发人员。谢谢!
更改为'[cell.contentView viewWithTag:101]'。所有的子视图都应该在单元格的内容视图中,而不是直接在单元格中。 – rmaddy 2014-09-25 22:47:26
我试过了,但它仍然返回零。还有什么我可能做错了? – user1585542 2014-09-25 22:51:22
你怎么知道viewWithTag:正在返回零?你是否确认使用日志记录或调试器? – rdelmar 2014-09-25 23:48:48