【发布时间】:2011-09-13 10:44:38
【问题描述】:
这应该很简单。
我有一个带有 TableView 的 iPhone 应用程序。如何将经典小箭头添加到每个单元格的右侧?
【问题讨论】:
标签: iphone objective-c ios cocoa-touch uitableview
这应该很简单。
我有一个带有 TableView 的 iPhone 应用程序。如何将经典小箭头添加到每个单元格的右侧?
【问题讨论】:
标签: iphone objective-c ios cocoa-touch uitableview
只需设置 UITableViewCell 的相应 accessoryType 属性即可。
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
在 Swift 3 中,
cell.accessoryType = .disclosureIndicator
【讨论】:
cell.accessoryType = .DisclosureIndicator 也适用于 Swift。
您可以在 Interface Builder 中执行此操作。只需点击Table View,转到右侧的Prototype Cells,将其设为1。然后点击Prototype Cell,然后在正确查找附件。在下拉菜单中,点击Disclosure Indicator。
【讨论】:
你可以设置小经典箭头,如下两种方式
1) 使用 UITableViewCell 的内置附件类型方法
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
2) 用自己的图片创建自己的附件视图
(I) 在您的项目中拖放一个箭头图像(即 circle_arrow_right.png)
(II) 每行的单元格设计方法如下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
编写以下代码:
if (cell ==nil) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
//For creating custom accessory view with own image
UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[accessoryView setImage:[UIImage imageNamed:@"circle_arrow_right.png"]];
[cell setAccessoryView:accessoryView];
[accessoryView release];
//Your other components on cells
.............
.............
}
[注意:为附件视图选择适当的图像并添加所需的单元格。为辅助视图选择小图像以获得更好的性能。]
【讨论】:
使用单元格的accessoryType 属性在右侧显示一个箭头。见this。
【讨论】:
对于简单的箭头:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
详细箭头:
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
【讨论】:
斯威夫特 3:
cell.accessoryType = .disclosureIndicator
【讨论】:
另一种方法是在创建单元格时指定UITableViewCellAccessoryDisclosureIndicator。为此,您可能会在 tableViewCellWithReuseIdentifier 委托方法中分配初始化您的单元格(然后继续自定义和配置您的单元格)。
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellAccessoryDisclosureIndicator reuseIdentifier:identifier];
【讨论】:
最好的方法是在Accessory 部分中选择Disclosure Indicator。
【讨论】: