UITableView浮动图像(视图)

时间:2014-02-06 21:17:36

标签: ios uitableview

我有UITableView覆盖整个屏幕,有很多单元格。 在包含UITableView的视图中,我想在右上角显示UIImage。 这个UIImage(或UIImageView)固定在UIView上 - 它不是tableView的一部分(按照z顺序,它在tableView上)。

即使在滚动时,也可以让UITableView浮动在此图像周围?

在我想要的结构的图像下面。想象一下,即使在图像下,它们也是更多的细胞。应该截断图像覆盖的单元格(或UILabels)(或进行换行)。

这可能吗?目前我不知道该怎么做。

enter image description here

1 个答案:

答案 0 :(得分:0)

一种选择是根据tableview的scrollview委托方法调整自定义表格视图单元格上标签的尾随空间约束。您需要检查您的单元格是否在imageView区域中,然后调整约束。您还需要在cellForRowAtIndexPath中调整约束以获得初始布局。

<强>故事板

enter image description here

<强> ViewController.m

#import "ViewController.h"
#import "MyCell.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];

    // Configure the cell...
    cell.myLabel.text = @"Lorem ipsum dolor sit amet, etc.";

    if (indexPath.row >=3 && indexPath.row <= 5) {

        cell.trailingSpaceConstraint.constant = 168;
    }

    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSUInteger count = 0;

    for (MyCell *cell in [self.tableView visibleCells]) {

        if (count >= 3 && count <= 6) {

            cell.trailingSpaceConstraint.constant = 168.0;
        } else {

            cell.trailingSpaceConstraint.constant = 20.0;
        }
        count++;
    }
}

@end

<强> MyCell.h

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *trailingSpaceConstraint;

@end

<强>输出

enter image description here

在此示例中,您需要使用自定义ViewController,添加tableView和imageView,钩子tableView的委托以及数据源和插座。您还需要添加UITableViewCell(MyCell)的自定义子类以及单元格标签和尾随空间约束的钩子出口。

在scrollViewDidScroll上检查行是快速而又脏的。可能更好的解决方案可能是检查哪些单元格在indexPathsForRowsInRect中触及imageView rect。

相关问题