我有Tableview
其中有A
到Z
的部分(我没有动态计算任何部分)
我想这样显示: : 我的数组值: msg_array = [“AajKaCatch”,“AajKaItem”,“Anari”,“Big C Mobiles”,“Big Flix”,“BigRock”,“caksonflowers,......”]
当我尝试在cellForRowAtIndexPath
中显示这样的内容时显示NSInvalidArgumentException
cell.textLabel.text=[[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"Merchant_Name"];
请提前帮助并致谢。
答案 0 :(得分:1)
当你这样做时:
[[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"Merchant_Name"];
您正在访问msg_array的元素,就好像它是一个包含NSDictionary
的NSArray。
但是,在msg_array中你只有NSStrings
。
您正在试图访问的结构是:
NSArray -> NSArray -> NSDictionary
你有
NSArray -> NSString
答案 1 :(得分:1)
你的数组就像:
array{object,object,object,object,object};
在这种情况下,你不能使用:
[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
因为要实现这样的一个,[msg_array objectAtIndex:indexPath.section]
应该返回一个数组。
所以实现这个,你需要尝试这样:
array{array{objects starts with 'A'},array{objects starts with 'B'},array{objects starts with 'C'}};
答案 2 :(得分:0)
我已经为联系信息和使用FKRSearchBarTableViewController的其他事情做了同样的事情,请参阅链接,下面是FKRSearchBarTableViewController的地下代码
- (id)initWithSectionIndexes:(BOOL)showSectionIndexes withDataSource:(NSArray*) dataSource withControllerId:(int) ControllerId forGroup:(int)groupId
{
if ((self = [super initWithNibName:nil bundle:nil])) {
self.title = @"Search Bar";
NSLog(@"%d",groupId);
_groupID = groupId;
_controllerId = ControllerId;
_showSectionIndexes = showSectionIndexes;
_famousPersons = [[NSMutableArray alloc]initWithArray:dataSource];
if (showSectionIndexes) {
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSMutableArray *unsortedSections = [[NSMutableArray alloc] initWithCapacity:[[collation sectionTitles] count]];
for (NSUInteger i = 0; i < [[collation sectionTitles] count]; i++) {
[unsortedSections addObject:[NSMutableArray array]];
}
if(ControllerId == 5)
{
for (Person *personName in self.famousPersons) {
// NSInteger index = [collation sectionForObject:[personName objectForKey:@"FirstName"] collationStringSelector:@selector(description)];
NSLog(@"%@",personName.firstName);
NSInteger index = [collation sectionForObject:personName.firstName collationStringSelector:@selector(description)];
[[unsortedSections objectAtIndex:index] addObject:personName];
}
}
else
{
for (NSDictionary *personName in self.famousPersons) {
NSInteger index = [collation sectionForObject:[personName objectForKey:@"FirstName"] collationStringSelector:@selector(description)];
[[unsortedSections objectAtIndex:index] addObject:personName];
}
}
NSMutableArray *sortedSections = [[NSMutableArray alloc] initWithCapacity:unsortedSections.count];
for (NSMutableArray *section in unsortedSections) {
[sortedSections addObject:[NSMutableArray arrayWithArray:[collation sortedArrayFromArray:section collationStringSelector:@selector(description)]]];
}
self.sections = [NSMutableArray arrayWithArray:sortedSections];
}
答案 3 :(得分:0)
使列表更具动态性,解决方案应该是
// given NSArray names = your full list of name
// NSArray indexes = your list of index
NSMutableArray *nameSections = [NSMutableArray arrayWithCapacity:26];
NSMutableArray *filteredIndexes = [NSMutableArray arrayWithCapacity:26];
for (NSString *index in indexes) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"SELF beginswith[c] %@",index];
NSArray *filterNames = [names filteredArrayUsingPredicate:predicate];
if(filterNames.count>0){
[nameSections addObject:filterNames];
[filteredIndexes addObject:index];
}
}
NSLog(@"filteredIndexes %@",filteredIndexes);
NSLog(@"nameSections %@",nameSections);
numOfSection = nameSections.count
numOfRow = [[numOfSection indexOfObject:section]count];
name = [[numOfSection indexOfObject:section]] indexOfObject:row];
// print log
//given indexes array a~z
names (
"a_string",
"a_string2",
"b_string",
"b_string2"
)
filteredIndexes (
a,
b
)
nameSections (
(
"a_string",
"a_string2"
),
(
"b_string",
"b_string2"
)
)