【发布时间】:2012-07-23 14:47:09
【问题描述】:
我有一个由 SQLite 数据库填充的 UITableView。 I added Section-based Grouping using the sectionIndexTitlesForTableView delegate method today and now when a Cell is selected, the String for indexPath.row is not the same as the text in the selected Cell.
我的代码是这样工作的。
- 我创建了一个数组来保存 SQLite 数据库中的业务。
- 我按字母顺序对该数组进行排序。
- 我只使用数据库中企业以字母开头的字母创建了一个字母数组。
- 我使用该 Array 和 NSPredicate 来提供 Grouped Header 视图,这些视图按业务的首字母按字母顺序进行分组。
- 将选定行写入 NSUserDefaults 文件,并推送视图控制器 (iPhone),或为该键添加观察者 (iPad)。
不幸的是,由于添加了标题视图,indexPath.row 现在返回的字符串与所选 Cell 的 TextLabel 的字符串完全不同,因此显示了不同的 Business 信息。
这里是主数组的重要代码块。
- (void)viewDidLoad
{
// Lots of code...
arrayName = [[NSMutableArray alloc] init];
NameSet = [[NSMutableSet alloc] init];
sortedArray = [arrayName sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
alphabet = [[NSMutableArray alloc] init];
[alphabet addObject:@"{search}"];
for (int i=0; i<[sortedArray count]-1; i++)
{
char alphabetUni = [[sortedArray objectAtIndex:i] characterAtIndex:0];
NSString *uniChar = [NSString stringwithFormat:@"%c", alphabetUni];
if (![alphabet containsObject:uniChar])
{
[alphabet addObject:uniChar];
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [alphabet count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows = 0;
NSString *alpha = [alphabet objectAtIndex:section];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alpha];
businesses = [sortedArray filteredArrayUsingPredicate:predicate];
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
rows = [self.searchResults count];
}
else {
rows = [businesses count];
}
return rows;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection (NSInteger)section
{
return [alphabet objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *alpha = [alphabet objectAtIndex:indexPath.section];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alpha];
businesses = [sortedArray filteredArrayUsingPredicate:predicate];
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
cell.textLabel.text =
[self.searchResults objectAtIndex:indexPath.row];
}
else{
NSString *cellValue = [businesses objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selected = nil;
if (tableView == self.tableView)
{
selected = [businesses objectAtIndex:indexPath.row];
}
else if (tableView == searchDis.searchResultsTableView)
{
selected = [filteredData objectAtIndex:indexPath.row];
}
[def setObject:selected forKey:@"NameChoiceDetail"];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
[self performSegueWithIdentifier:@"NameDetailPush" sender:self];
}
}
// 请原谅我写得很糟糕的代码。我只使用了 Objective-C 4 个月,而编程大约 8 个月。任何建议/优化都会被适当记录。
【问题讨论】:
-
真的是随机的吗?尝试在与字符串有任何关系的每个步骤中添加 NSLogs。在您的 didSelectRowAtIndexPath 中,您正在执行 indexPath = [businesses objectAtIndex:indexPath.row],但在 cellForRow 中您正在执行 NSString *cellValue = [businesses objectAtIndex:indexPath.row] ,这看起来是错误的。部分没有被检查。 if (indexPath.section = someValue) 然后做一些具体的事情。表格视图中每个部分的 indexPath.row 值从零开始
-
我不知道为什么要在其中设置 indexPath 值的断言。我想我正在涉足它,认为这会解决它。请放心,它不在当前(正在工作的)代码中,并且已被编辑出问题。
标签: iphone sqlite uitableview nspredicate didselectrowatindexpath