簡體   English   中英

UIView子類不可見

[英]UIView subclass is not visible

我正在創建UIView(MultiColumnTableView)的子類,它將包含幾個表視圖。 但是,當我將自定義視圖作為子視圖添加到視圖控制器時,它從不可見,我真的不知道為什么嗎?

我在我的根視圖控制器中添加了以下代碼:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _multiColumnTableView = [[MultiColumnTableView alloc] initWithNumberOfColums:3 columnWidth:200 frame:CGRectMake(100, 100, 0, 400)];
    _multiColumnTableView.dataSource = self;
    _multiColumnTableView.delegate = self;


    [self.view addSubview:_multiColumnTableView];
    [_multiColumnTableView reloadData];
}

定制類初始化程序包含以下代碼:

- (id)initWithNumberOfColums:(NSInteger)columns columnWidth:(float)width frame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.numberOfColumns = columns;
        self.columnWidth = columnWidth;

        self.bounds = CGRectMake(0, 0, columnWidth * numberOfColumns, self.bounds.size.height);

        _tableViews = [NSMutableArray new];

        // Create all the desired colums (= tableViews)
        for (int i = 0; i < numberOfColumns; i++) {
            UITableView *t = [[UITableView alloc] initWithFrame:CGRectMake(columnWidth * i, 0, columnWidth, self.bounds.size.height)];
            [_tableViews addObject:t];
            t.tag = i;
            t.backgroundColor = [UIColor blueColor];
            t.dataSource = self;
            t.delegate = self;
            [self addSubview:t];
        }

    }
    return self;
}

我期望看到一些藍表視圖,但它們不可見,因此它們從不調用cellForRowAtIndexPath :,但它們確實調用numberOfRowsInSection。 我的自定義子視圖已添加到根視圖。 計算子視圖時,它返回1。有人可以看到問題嗎?

檢查表格視圖的每個框架是否已正確分配。

在構造函數中進行以下更改。

- (id)initWithNumberOfColums:(NSInteger)columns columnWidth:(float)width frame:(CGRect)frame
{
     self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, columns*width, frame.size.height)];
     if (self) {
          self.numberOfColumns = columns;
          self.columnWidth = columnWidth;


          _tableViews = [NSMutableArray new];

               // Create all the desired colums (= tableViews)
          for (int i = 0; i < numberOfColumns; i++) {
               UITableView *t = [[UITableView alloc] initWithFrame:CGRectMake(columnWidth * i, 0, columnWidth, frame.size.height)];
               [_tableViews addObject:t];
               t.tag = i;
               t.backgroundColor = [UIColor blueColor];
               t.dataSource = self;
               t.delegate = self;
               [self addSubview:t];
          }

     }
     return self;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM