-2

次のコードを使用して 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;
        }
4

3 に答える 3

0

eventDrawer.bounds = eventCell.bounds;試す代わりにeventDrawer.frame = eventCell.bounds;

ビューのboundsは内側から見られるため、原点は 0, 0 になります。ただし、frameビューの はそのスーパービューに関連しているため、原点は 0, 0 とは異なる場合があります。

于 2013-08-18T06:33:02.540 に答える
0

次の行が問題です。

// Grab the selected cell and make sure it's an event cell
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

UITableViewDataSource から新しいセルを取得しています! 代わりに、UITableView からセルを直接取得する必要があります。

// Grab the selected cell and make sure it's an event cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

乾杯!

于 2013-08-19T19:31:38.827 に答える