繁体   English   中英

单击单元格时,UITableView顶部的UIView?

[英]UIView on top of a UITableView when cell is clicked?

我想做的是单击UITableView时在UITableView顶部显示一个smaller view 我不想过渡到另一个UIViewController,而是在UITableView顶部“ popup ”一个视图,该视图将包含有关所单击的UITableViewCell的更多信息。

我想我不是在寻找UIAlertView,而是在寻找可以贴标签,按钮,图片等的UIView。

我希望我的术语是正确的。 我还是个新手:)

莱昂

ps我所有的搜索都只是想出UIAlertView的东西。

尝试使用UITableView的didSelectRowAtIndexPath委托函数,未经测试的代码给您一个想法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Assuming view is at zero index of XIB file.
    // this view will contain all lable and other controls
    UIView *customView = (UIView *)[[NSBundle mainBundle] loadNibNamed:@"NameOfCustomViewXIBFile" owner:nil options:nil] objectAtIndex:0];

    customView.transform = CGAffineTransformMakeScale(0.0f, 0.0f);
    [self.view addSubView:customView];

    [UIView animateWithDuration:0.5
                     animations:
                  ^{
                          customView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
                       }
    ];   
}

希望能帮助到你!


编辑:

删除此弹出窗口的动画:

    customView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);

    [UIView animateWithDuration:0.5
                     animations:
                  ^{
                          customView.transform = CGAffineTransformMakeScale(0.0f, 0.0f);
                       }
    ];   
    [customView removeFromSuperView];

现有答案的问题在于,如果用户滚动表格以使该单元格脱离屏幕并重新打开,则由于单元格的重复使用,您弹出的视图可能会消失。 为了避免这种情况,您必须在单元格的“模型”中存储一些内容,以便每次调用cellForRowAtIndexPath:时,如果应该在单元格中使用其弹出视图来重新生成该单元格。 仅从didSelectRowAtIndexPath:中显示可能不够。

如果要进行编码,我更喜欢的方法是使所有单元格成为UITableViewCell自己的子类。 该子类包含一个额外的BOOL showPopup。 在setter setShowPopup中:设置值,并创建和显示或破坏/删除/隐藏该单元格的“弹出”子视图。 (您可以使子视图成为始终存在的类成员,在需要时进行分配和分配,并保留引用,仅在需要时显示/隐藏;这是尺寸/空间性能的折衷。)

-(void) setShowPopup:(BOOL show) {
    showPopup = show;
    if(show) {
        // create subview, and add to view
    } else {
        // remove and destroy subview
    }
}

并在UITableView委托/数据源中:

-(UITableViewCell) UITableView:(UITableView *table) cellForRowAtIndexPath:(NSIndexPath *ip) {
    // usual stuff to get a reusable cell or allocate a new one, and prepare it for display
    // then
    if(showPopup) {
       // create subview and add to view
    }   else {
       // remove and destroy subview
    }
}

我在GLKViewController中将UIButton用于类似的东西,因此这可能适用于UITableViewController。 我只是禁用了该按钮,并添加了一个圆形边框,使其看起来像一个自定义弹出窗口。 当用户单击一些有趣的东西时,我更改了按钮上的隐藏标志以显示或隐藏它。

像这样的东西使它看起来弹出

在viewDidLoad中:

// make info "popup"
_infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_infoBtn.bounds = CGRectMake( 0, 0, kRightButtonBarDim, kRightButtonBarDim);
_infoBtn.backgroundColor = [UIColor colorWithWhite:1 alpha:0.75];
_infoBtn.selected = NO;
_infoBtn.titleLabel.font = [UIFont boldSystemFontOfSize:kTableCellFontSize];
_infoBtn.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
[_infoBtn setTitleColor:[UIColor colorWithWhite:.3 alpha:1] forState:UIControlStateDisabled];

// border
_infoBtn.layer.cornerRadius = 5;                             // rounded corners

// drop shadow
_infoBtn.layer.masksToBounds = NO;
_infoBtn.layer.shadowColor = [UIColor blackColor].CGColor;
_infoBtn.layer.shadowOpacity = 0.8;
_infoBtn.layer.shadowRadius = 12;
_infoBtn.layer.shadowOffset = CGSizeMake(12.0f, 12.0f);

_infoBtn.enabled = NO;
_infoBtn.hidden = YES;

[self.view addSubview:_infoBtn];

然后在tableView的didSelectRowAtIndexPath中更新按钮中所需的任何信息,并将其隐藏为NO。 您应该能够将任何必要的子视图添加到此UIButton。

_infoBtn.frame = CGRectMake(20, 20, 20, 20);    // set this wherever you like
[_infoBtn setTitle:@"text" forState:UIControlStateDisabled];
_infoBtn.hidden = NO;

我刚刚针对具有UITableView作为子视图的UIViewController进行了测试,它看起来相同。 滚动没有影响它(即,按钮没有随着滚动而移动,几乎像HUD),因为该按钮已添加到顶级UIView中。 如果您需要的是Dunno,但希望它能给您一些想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM