viewWithTag返回零在cellForRowAtIndexPath方法
我有一个自定义原型单元格的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#开发人员。谢谢!
我给你一个选择。为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在故事板的细胞。
希望这会有所帮助!
感谢您的回复。该物业应该是IBOutlets?我如何把它们连接起来? – user1585542 2014-09-26 00:01:42
我已经实现了这一点,并认识到,当我切换我的iPhone风景它的作品,但是当我在肖像模式下它不起作用。任何想法为什么会发生?谢谢!! – user1585542 2014-09-26 00:21:52
你能把它们连接起来吗? Command +单击并从所有者(即具有黄色图标的viewController)拖到子视图(标签,图像视图等)。 纵向模式下不起作用? – Leandro 2014-09-26 01:19:42
更改为'[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