2017-04-10 77 views
-1

我用Swift编写了一个项目(在项目中使用此项目之前编写)。我在程序中遇到了一个问题:使用swift将tableView添加到自定义视图

自定义视图有一个按钮,点击按钮会弹出一个列表,点击其中一个单元格列表,按钮的内容就会变成单元格的内容。据了解,该代码是用Objective C的

@property (nonatomic, weak) UITableView *tableView; 

@property (weak, nonatomic) IBOutlet UIButton *cityBtn; 

- (UITableView *)tableView { 

    if (_tableView == nil) { 
    UITableView *tableView = [[UITableView alloc] init]; 
    tableView.dataSource = self; 
    tableView.delegate = self; 
    tableView.backgroundColor = [UIColor blackColor]; 
    tableView.alpha = 0.9; 
    tableView.x = self.cityBtn.x; 
    tableView.y = CGRectGetMaxY(self.cityBtn.frame); 
    tableView.width = self.cityBtn.width; 
    tableView.heigth = 0; 
    tableView.layer.cornerRadius = 10; 
    [self addSubview:tableView]; 
    _tableView = tableView; 

} 

return _tableView; 

} 

请问这个代码是如何转换成斯威夫特

回答

0
var tableView = UITableView();//or TableView 
tableView.dataSource = self; 
tableView.delegate = self; 
tableView.backgroundColor = UIColor.black; 
tableView.alpha = 0.9; 
tableView.x = self.cityBtn.x; 
tableView.y = CGRectGetMaxY(self.cityBtn.frame); 
tableView.width = self.cityBtn.width; 
tableView.heigth = 0; 
tableView.layer.cornerRadius = 10; 
self.addSubview(tableView); 
_tableView = tableView; 
0

我面临同样的问题很久以前。下面列出了一些很好的参考资料,以获得帮助。

Otherthen您convered代码。

if tableView == nil { 
    var tableView = UITableView() 
    tableView.dataSource = self 
    tableView.delegate = self 
    tableView.backgroundColor = UIColor.black 
    tableView.alpha = 0.9 
    tableView.x = cityBtn.x 
    tableView.y = cityBtn.frame.maxY 
    tableView.width = cityBtn.width 
    tableView.heigth = 0 
    tableView.layer.cornerRadius = 10 
    addSubview(tableView) 
    tableView = tableView 
} 
return tableView 

谢谢。

+0

我原本是这样写的,但执行后没有显示它 – RaymondWoo

+0

嗨,我已经实现了代码:beetxt.com/2F3。如果有任何错误,请提出查询。谢谢。 –

相关问题