如何将目录树添加到表视图?
How can I add a directory tree to a tableview?
如何将目录树添加到表视图?
我不知道它是怎么叫的。是否有默认元素,还是我必须从头开始制作?
UITableViewController
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 2;
} else {
return 1;
}
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView
cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
UITableViewCell *cellView = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cellView) {
cellView = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
}
return cellView;
}
没有“内置树视图”类型的 UI 组件,但您可以使用单元格的 .indentationLevel
和 .indentationWidth
属性来实现。
这是一个非常简单的例子:
struct IndentDataStruct {
var level: Int = 0
var title: String = ""
}
class TreeTableViewController: UITableViewController {
var myData: [IndentDataStruct] = []
override func viewDidLoad() {
super.viewDidLoad()
var ids: IndentDataStruct!
ids = IndentDataStruct(level: 0, title: "Favorites")
myData.append(ids)
for i in 1...2 {
ids = IndentDataStruct(level: 1, title: "Favorite \(i)")
myData.append(ids)
}
ids = IndentDataStruct(level: 0, title: "Bookmarks")
myData.append(ids)
for i in 1...3 {
ids = IndentDataStruct(level: 1, title: "Bookmark \(i)")
myData.append(ids)
}
ids = IndentDataStruct(level: 0, title: "Others")
myData.append(ids)
for i in 1...2 {
ids = IndentDataStruct(level: 1, title: "Other \(i)")
myData.append(ids)
for j in 1...2 {
ids = IndentDataStruct(level: 2, title: "Other Sub \(i) - \(j)")
myData.append(ids)
}
}
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "defaultCell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = tableView.dequeueReusableCell(withIdentifier: "defaultCell", for: indexPath)
let d = myData[indexPath.row]
c.textLabel?.text = d.title
c.indentationLevel = d.level
// just to show different indent widths...
c.indentationWidth = d.level == 2 ? 32 : 16
return c
}
}
输出为:
请注意,该示例使用默认值 UITableViewCell
class,它会自动处理缩进。
如果您使用的是自定义单元格,则需要自行调整。
假设您将单元格的元素限制为单元格的 contentView.layoutMarginsGuide
(您应该这样做),您需要实现:
override func layoutSubviews() {
super.layoutSubviews()
contentView.layoutMargins.left = CGFloat(indentationLevel) * indentationWidth
}
如何将目录树添加到表视图?
我不知道它是怎么叫的。是否有默认元素,还是我必须从头开始制作?
UITableViewController
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 2;
} else {
return 1;
}
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView
cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
UITableViewCell *cellView = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cellView) {
cellView = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
}
return cellView;
}
没有“内置树视图”类型的 UI 组件,但您可以使用单元格的 .indentationLevel
和 .indentationWidth
属性来实现。
这是一个非常简单的例子:
struct IndentDataStruct {
var level: Int = 0
var title: String = ""
}
class TreeTableViewController: UITableViewController {
var myData: [IndentDataStruct] = []
override func viewDidLoad() {
super.viewDidLoad()
var ids: IndentDataStruct!
ids = IndentDataStruct(level: 0, title: "Favorites")
myData.append(ids)
for i in 1...2 {
ids = IndentDataStruct(level: 1, title: "Favorite \(i)")
myData.append(ids)
}
ids = IndentDataStruct(level: 0, title: "Bookmarks")
myData.append(ids)
for i in 1...3 {
ids = IndentDataStruct(level: 1, title: "Bookmark \(i)")
myData.append(ids)
}
ids = IndentDataStruct(level: 0, title: "Others")
myData.append(ids)
for i in 1...2 {
ids = IndentDataStruct(level: 1, title: "Other \(i)")
myData.append(ids)
for j in 1...2 {
ids = IndentDataStruct(level: 2, title: "Other Sub \(i) - \(j)")
myData.append(ids)
}
}
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "defaultCell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = tableView.dequeueReusableCell(withIdentifier: "defaultCell", for: indexPath)
let d = myData[indexPath.row]
c.textLabel?.text = d.title
c.indentationLevel = d.level
// just to show different indent widths...
c.indentationWidth = d.level == 2 ? 32 : 16
return c
}
}
输出为:
请注意,该示例使用默认值 UITableViewCell
class,它会自动处理缩进。
如果您使用的是自定义单元格,则需要自行调整。
假设您将单元格的元素限制为单元格的 contentView.layoutMarginsGuide
(您应该这样做),您需要实现:
override func layoutSubviews() {
super.layoutSubviews()
contentView.layoutMargins.left = CGFloat(indentationLevel) * indentationWidth
}