我正在尝试为我的uitableview设置索引和部分,我已经设法做了..但是现在我试图将我的NSDictionary值传递给我的uitableviewcell我的应用程序在我尝试访问NSDictionary时崩溃了在我从中传递值的方法之外。
我在想,也许我没有正确地传递这些值或者其他的东西,但我根本无法弄清楚为什么会这样......
继承我的代码......
·H
@interface VehicleResultViewController : UITableViewController <NSXMLParserDelegate> {
//......
//Indexed tableview stuff
NSArray *sortedArray;
NSMutableDictionary *arraysByLetter;
NSMutableArray *sectionLetters;
}
//.....
//Indexed tableview stuff
@property (nonatomic, retain) IBOutlet NSArray *sortedArray;
@property (nonatomic, retain) IBOutlet NSMutableDictionary *arraysByLetter;
@property (nonatomic, retain) IBOutlet NSMutableArray *sectionLetters;
//....
的.m
//...
//This is where I try to access the NSDictionary to pass it to my uitableviewcells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryNone; //make sure their are no tickes in the tableview
cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection
// Configure the cell...
NSString *value = [self.arraysByLetter objectForKey:[[self.arraysByLetter allKeys] objectAtIndex:indexPath.row]];
cell.textLabel.text = key;
NSLog(@"%@",arraysByLetter);
return cell;
}
//This is where I set NSDictionary
//method to sort array and split for use with uitableview Index
- (IBAction)startSortingTheArray:(NSMutableArray *)arrayData
{
//Sort incoming array alphabetically
sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
//NSLog(@"%@",sortedArray);
// Dictionary will hold our sub-arrays
arraysByLetter = [NSMutableDictionary dictionary];
sectionLetters = [[NSMutableArray alloc] init];
// Iterate over all the values in our sorted array
for (NSString *value in sortedArray) {
// Get the first letter and its associated array from the dictionary.
// If the dictionary does not exist create one and associate it with the letter.
NSString *firstLetter = [value substringWithRange:NSMakeRange(0, 1)];
NSMutableArray *arrayForLetter = [arraysByLetter objectForKey:firstLetter];
if (arrayForLetter == nil) {
arrayForLetter = [NSMutableArray array];
[arraysByLetter setObject:arrayForLetter forKey:firstLetter];
[sectionLetters addObject:firstLetter]; // This will be used to set index and section titles
}
// Add the value to the array for this letter
[arrayForLetter addObject:value];
}
// arraysByLetter will contain the result you expect
NSLog(@"Dictionary: %@", arraysByLetter); //This prints what is currently in the NSDictionary
//Reloads data in table
[self.tableView reloadData];
}
。在上面的最后一个方法中用NSLog检查字典的输出
Dictionary: {
H = (
Honda,
Honda,
Honda,
Honda,
Honda,
Honda,
Honda
);
M = (
Mazda,
Mazda,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi
);
N = (
Nissan,
Nissan,
Nissan,
Nissan,
Nissan,
Nissan,
Nissan
);
T = (
Toyota,
Toyota,
Toyota
);
}
我已经调试了两个点(我在方法中设置了NSdictionary)和(我在cellforrowatindexpath中访问NSDictionary)并在我尝试使用它之前进行了默认设置。
任何帮助将不胜感激..
答案 0 :(得分:2)
//Indexed tableview stuff
@property (nonatomic, retain) NSArray *sortedArray;
@property (nonatomic, retain) NSMutableDictionary *arraysByLetter;
@property (nonatomic, retain) NSMutableArray *sectionLetters;
从属性声明中删除 IBOutlet。它仅适用于Interface Builder控件。
同样纠正dictonary分配 - self.arraysByLetter = [NSMutableDictionary dictionary];
答案 1 :(得分:2)
分配的NSMutableDictionary
为autoreleased
,因此当在其他方法中调用NSAutoreleasePool
时,NSMutableDictionary
已被耗尽,released
已self.arraysByLetter = [NSMutableDictionary dictionary];
}}。如果你想使用属性保留对象,你必须这样做:
self
retain
将使用声明为autoreleased
的setter设置字典,因此当您稍后尝试使用它时它将可用。
请注意,不以new或alloc或contains副本开头的任何方法都必须返回{{1}}对象,这是您的情况。