我有两个tableview,每个tableview只包含一个单元格,现在我想在这两个tableviews的单元格中添加一个UIWebView
- (void)loadView
{
interestView = [[UIWebView alloc] initWithFrame:_webViewFrame];
interestView.delegate = self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
//interestView is UIWebView
if (tableView == interestTableView) {
[cell.contentView addSubview:interestView];
}else if (tableView == headerTableView) {
[cell.contentView addSubview:interestView];
}
}
但这不起作用,两个表格单元格都没有显示UIWebView。有什么想法吗?
答案 0 :(得分:0)
您需要先设置UIWebView
的框架,然后再将其添加为UITableViewCell
的子视图。
interestView.frame = CGRectMake(0,0,cell.contentView.frame.size.width,cell.contentView.frame.size.height);
然后当你添加它时,你应该能够看到它吗?
答案 1 :(得分:0)
为什么不创建一个自定义表视图单元格,将uiwebview作为子视图,然后将其添加到表视图中,而不是您正在使用的默认UITableViewCell。如果您在xib中定义自定义单元格,则无需从代码中添加子视图或更新框架。