我遇到了填充UITableView
的问题。
我有一个NSMutableArray
customers
。它看起来像这样:
customer
first letter="A"
customer name="Adwars Inc."
customer
first letter="A"
customer name="Amman Co."
customer
first letter="B"
customer name="Building Inc."
customer
first letter="C"
customer name="Computer Co."
所以我有一个对象客户,它将每个客户分开。我为每个对象都有一些键。 在我的第二个NSArray中,我收到了我的第一封信,这些信件出现在我的客户数据中。它看起来像这样:
A
B
C
D
G
J
M
S
Z
我能够在部分中获得正确的部分计数和行,但是当我尝试填充表格视图时,它总是如下所示:
这是我的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"CustomerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
for(int i = 0; i < [firstletters count]; i++)
{
if (indexPath.section == i) {
for(int count = 0 ;count < [customers count]; count++)
{
NSString *firstletter;
NSString *key;
key = [firstletters objectAtIndex:indexPath.section];
firstletter = [[customers objectAtIndex:count] objectForKey: @"FirstLetter"];
if ([key isEqualToString:firstletter]) {
cell.textLabel.text = [[customers objectAtIndex:count] objectForKey: @"S_NAME1"];
cell.detailTextLabel.text = [[customers objectAtIndex:count] objectForKey: @"s_town"];
}
}
}
}
return cell;
}
我需要做些什么才能让它发挥作用?
答案 0 :(得分:1)
我知道你已经接受了答案,但我只想提出另一个想法,即如何以不同的方式构建数据。如果你有一个字典,其中的键是客户名字的第一个字母,而值是第一个字母与键相同的客户对象,那么你就不必进行任何循环(我不是知道你是否还在解决方案中这样做了。我做了一个示例项目(看看这是否可行)以这种方式构建数据,除了我的“对象”只是公司的名称而不是客户对象。在我的表视图控制器中,我有:
- (void)viewDidLoad {
[super viewDidLoad];
self.companyDict = [NSMutableDictionary dictionary];
NSArray *aArray = [NSArray arrayWithObjects:@"Abercrombie & Fitch",@"Altera",@"Agilent",@"Allelix",@"Abbott Laboratories", nil];
NSArray *cArray = [NSArray arrayWithObjects:@"CocaCola",@"Continental",@"ConocoPhillips", nil];
NSArray *mArray = [NSArray arrayWithObjects:@"Myriad Genetics",@"Myrexis",@"Microsoft",@"McDonald's", nil];
NSArray *nArray = [NSArray arrayWithObjects:@"Nokia",@"NPS Pharmaceuticals",@"Norelco",@"Netflix",@"Nextel",@"Navistar International", nil];
[self.companyDict setValue:aArray forKey:@"A"];
[self.companyDict setValue:cArray forKey:@"C"];
[self.companyDict setValue:mArray forKey:@"M"];
[self.companyDict setValue:nArray forKey:@"N"];
self.keys = [[self.companyDict allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.keys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.companyDict valueForKey:[self.keys objectAtIndex:section]]count];
}
- (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];
}
NSArray *theArray = [self.companyDict valueForKey:[self.keys objectAtIndex:indexPath.section]];
cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
return cell;
}
答案 1 :(得分:0)
尝试(复制并粘贴此代码):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"CustomerCell"] autorelease];
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
}
for(int i = 0; i < [firstletters count]; i++) {
if (indexPath.section == i) {
for(int count = 0 ;count < [customers count]; count++) {
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
NSString *firstletter = nil;
NSString *key = nil;
key = [firstletters objectAtIndex:indexPath.section];
firstletter = [[customers objectAtIndex:count] objectForKey: @"FirstLetter"];
if ([key isEqualToString:firstletter]) {
cell.textLabel.text = [[customers objectAtIndex:count] objectForKey: @"S_NAME1"];
cell.detailTextLabel.text = [[customers objectAtIndex:count] objectForKey: @"s_town"];
}
}
}
}
return cell;
}
答案 2 :(得分:0)
对于每个单元格,您将遍历所有客户,为每个客户重复设置(并重置)textLabel和detailTextLabel,其第一个字母与当前部分匹配(但您没有考虑)该部分中该客户的索引是否与当前indexPath.row匹配)。这意味着在您的代码中,每个单元格的第一个字母与当前部分的第一个字母匹配的最后一个客户将拥有textLabel和detailTextLabel。
答案 3 :(得分:0)
试试这个TableKit库。 这种情况下解决方案将是干净而优雅的:
NSMutableDictionary* sectionMap = [NSMutableDictionary dictionaryWithCapacity:30];
for(NSDictionary* c in customers) {
NSString* firstLetter = [c objectForKey:@"FirstLetter"];
NSString* name = [c objectForKey:@"S_NAME1"];
NSString* town = [c objectForKey:@"s_town"];
TKSection* section = [sectionMap objectForKey:firstLetter];
if(!section) {
section = [TKSection sectionWithCells:nil];
section.headerTitle = firstLetter;
[sectionMap setObject:section forKey:firstLetter];
}
TKCell* cell = [TKStaticCell cellWithStyle:UITableViewCellStyleSubtitle text:name detailText:town];
[section addCell:cell];
}
self.sections = [sectionMap allValues];