开发者

UITableView add subview

I want to add subview in UITableView

I tried like this way:

[self.tableView addSubView:myView.view];

I didn't got any error but view is not getting added, can anyone please he开发者_开发技巧lp..?

Thanks.


Try this:

view.frame = CGRectMake(view.frame.origin.x, -view.frame.size.height, view.frame.size.width, view.frame.size.height);
[self.tableView addSubview:view];
[self.tableView setContentInset:UIEdgeInsetsMake(view.frame.size.height, 0, 0, 0)];

this will add the view above the cells and move the cells down by the height of the added view.


I am no getting why you are adding subview to Tableview. May be you want to add something to each cell of tableView. You can do this by adding:

[cell.contentView addSubview:yourSubView];

in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
}


Both of self.tableView and myView should not be nil

What do you mean myView.view?

this piece of code works good for me:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 200, 20)];
labelView.text = @"TEST";
[headerView addSubview:labelView];

[self.tableView addSubview:headerView];


For Swift:

let foregroundView = UILabel(frame: CGRectMake(0, 0, tableView.bounds.size.width, tableView.bounds.size.height))
foregroundView.backgroundColor = UIColor.whiteColor()
tableView.addSubview(foregroundView)
foregroundView.bringSubviewToFront(tableView)

You can remove it also:

foregroundView.removeFromSuperview()


Simply replace your line of code with the one below:

[self.tableView.superview addSubview:myView];

Note:

I am not certain about myView.view in line of code you mentioned in your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜