次のコードを使用して aUIView
から aをインスタンス化しXIB
、それを選択UITableViewCell
したものに追加して表示しています。コードを実行してセルに触れると、コードを一通り見て、すべてが正しくインスタンス化されている (何も nil ではない) ことを確認できますが、ビューは表示されません。
いくつかのボタンが付いた UIView があります。Interface Builder で、UIView が UIView のサブクラスを使用するように設定しました。このサブクラスには、Xcode によって生成されたボイラー プレート以外に、現時点ではコードが含まれていません。このコードを使用してこれを機能させる際に犯した明らかなエラーを誰かが指摘してくれることを願っています。
ある時点で UITalbeViewCell 内に UIView を表示していたことに注意してください。しかし、いくつかのリファクタリング中にいくつかのものを台無しにしてしまい、これを処理するためにコードを書き直すことになりました。そうなると、セル内に UIView を表示できなくなりました。
@implementation HZRunwayViewController
{
EKEvent *currentEvent;
BOOL editingEvent;
HZEventDrawerView *eventDrawer;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Check if we are touching an event
if (indexPath.section == 0 && [self.eventsForCurrentDay count]) {
// Grab the selected cell and make sure it's an event cell
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[HZEventTableViewCell class]]) {
// Setup our event cell and our action drawer for use.
HZEventTableViewCell *eventCell = (HZEventTableViewCell *)cell;
if (!eventDrawer) {
eventDrawer = (HZEventDrawerView *)[[NSBundle mainBundle] loadNibNamed:@"HZEventDrawerView" owner:self options:nil][0];
}
eventDrawer.bounds = eventCell.bounds;
NSLog(@"X:%f Y:%f Width: %f Height: %f", eventDrawer.bounds.origin.x, eventDrawer.bounds.origin.y, eventDrawer.bounds.size.width, eventDrawer.bounds.size.height);
[eventCell.contentView addSubview:eventDrawer];
[eventCell bringSubviewToFront:eventDrawer];
eventDrawer.hidden = NO;
}
}
}
アップデート
XIB ファイルまたはサブクラスが問題の原因であるかどうかをテストして確認するために、使用を中止し、UIView をインスタンス化し、UIButton を追加してから、Cell.contentView サブビューに追加しました。セルに触れても何も起こりません。
// Grab the selected cell and make sure it's an event cell
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[HZEventTableViewCell class]]) {
// Setup our event cell and our action drawer for use.
HZEventTableViewCell *eventCell = (HZEventTableViewCell *)cell;
if (!eventDrawer) {
eventDrawer = [[UIView alloc] initWithFrame:eventCell.bounds];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 25, 44)];
[eventDrawer addSubview:button];
}
eventDrawer.frame = eventCell.bounds;
NSLog(@"X:%f Y:%f Width: %f Height: %f", eventDrawer.bounds.origin.x, eventDrawer.bounds.origin.y, eventDrawer.bounds.size.width, eventDrawer.bounds.size.height);
[eventCell.contentView addSubview:eventDrawer];
[eventCell bringSubviewToFront:eventDrawer];
eventDrawer.hidden = NO;
}