2015-10-07 56 views
-1

我尝试实现索引tableview与数字,但我无法实现,因为它在我的代码中有一些问题,所以请帮我实现该?如何使用Objective-C中的数字实现索引tableview?

这里我已经声明在字符串中的数字,但我想获得数组,所以请说如何获得这些值在数组中?

这就是我试图代码...

NSString *numbers = @"100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000"; 
self.tableData = [numbers componentsSeparatedByString:@" "]; 
numbers = @"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20"; 
self.indexTitlesArray = [numbers componentsSeparatedByString:@" "]; 
+1

你面临哪个问题?你可以把一些错误日志或更多描述你面临的问题吗? – KTPATEL

+0

我在这段代码中没有错误,我想获得数组,但我不能。 –

回答

1

做这样

NSArray *self.indexTitlesArray = @[@"1", @"2",@"3", @"4",@"5", @"6",@"7", @"8",@"9", @"10",@"11", @"12",@"13", @"14",@"15", @"16",@"17",@"18",@"19",@"20", nil]; 

委托方法是

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    return self.indexTitlesArray; 
} 

额外reference

你NE编辑相同的答案

NSString *numbers = @"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20"; 
NSArray *arr = [numbers componentsSeparatedByString:@" "]; 
self.indexTitlesArray = [arr mutableCopy]; 

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
    { 
    return self.indexTitlesArray; 
    } 

输出像在控制台

enter image description here

+0

我会尝试你的想法.. –

+0

@Saravanakumar - 我展示了2种方法,它的工作100%肯定 –

+0

好吧@ anbu.karthik ..现在我正在尝试你的方法。 –

0

您已经使用componentsSeparatedByString获得从给定的字符串分隔每个号码。

它总是返回包含子串的数组。如果你想获得像NSInteger或int这样的其他数据类型的结果数组,那么你必须手动完成。您可以为循环中的每个元素运行循环,并执行从NSString到int的类型转换。从的NSString

类型铸造到int:

int dataValue = [@"100" intValue]; 

下面是componentsSeparatedByString :参考的的NSString

返回包含从所述接收器子阵列已经由一个给定的分离器分开。

宣言

- (NSArray<NSString *> * _Nonnull)componentsSeparatedByString:(NSString * _Nonnull)separator 

参数分隔:分隔字符串。

返回值:一个NSArray对象,包含接收器中已被分隔符分隔的子字符串。

讨论:

数组中的子串出现在他们的接收器那样的顺序。相邻的分隔符字符串会在结果中产生空字符串。同样,如果字符串以分隔符开头或结尾,则第一个或最后一个子字符串分别为空。

例如,该代码段:

NSString *list = @"Karin, Carrie, David"; 

NSArray *listItems = [list componentsSeparatedByString:@", "]; 

产生一个阵列{ @"Karin", @"Carrie", @"David" }.

如果list用逗号和空间开始 - 例如,@", Norman, Stanley, Fletcher" - 所述阵列具有这些内容:{ @"", @"Norman", @"Stanley", @"Fletcher" }

如果列表中没有分隔符 - 例如,"Karin"该数组包含字符串本身,在这种情况下为{ @"Karin" }

你可以找到更多的细节here

相关问题