【发布时间】:2015-06-23 11:50:55
【问题描述】:
【问题讨论】:
-
你能举一些例子如何需要
-
是的,我也更新了。 @安布
标签: ios iphone uitableview scrollbar uislider
【问题讨论】:
标签: ios iphone uitableview scrollbar uislider
我按照以下方式使用它来显示字母滚动条,并在单击任何字母时转到该部分。 注意:我的表格视图有 n 个部分,每个部分只有 1 行(这是我的要求)。因此,单击任何字符将滚动到该部分。 这也可以解决您单击没有匹配结果的字母表的情况。
例如,您点击F。 indexSectionTitles 中没有以F 开头的条目,直到 D ( Doll )。所以我会尝试移动到F 之前的部分,即E。所以这继续下去,使用将被带到最接近它之前的部分。
var indexTitles = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
var indexSectionTitles: [String] = ["Apple", "Ball", "Car", "Doll"]
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
if indexSectionTitles.indexOf(title) != nil {
return indexSectionTitles.indexOf(title)!
}
else {
let char = title as NSString
let prevCharIndex = getPrevName(char)
print("prevCharIndex; ", prevCharIndex)
return prevCharIndex
}
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return indexTitles
}
func getPrevName(title: NSString) -> Int {
if title.isEqualToString("") {
return 0
}
let charInt = title.characterAtIndex(0) - 1
let charStr = String(UnicodeScalar(charInt))
if indexSectionTitles.indexOf(charStr) != nil {
return indexSectionTitles.indexOf(charStr)!
}
return getPrevName(charStr)
}
【讨论】:
@interface yourViewcontroller ()
{
NSArray * IndexTitles;
}
- (void)viewDidLoad
{
[super viewDidLoad];
IndexTitles = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return IndexTitles;
}
// - (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index
// {
// return index;
// }
需要参考请关注此tutorial
【讨论】:
sectionForSectionIndexTitle 方法以返回一个整数(这是部分编号),当您单击一个字符时要转到该值。看我的回答