0

我遇到了填充UITableView. 我有一个NSMutableArraywith 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;
}

我该怎么做才能让它工作?

4

4 回答 4

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;
}
于 2012-04-30T01:45:39.793 回答
0

对于每个单元格,您都在遍历所有客户,为首字母与当前部分匹配的每个客户重复设置(和重置)textLabel 和 detailTextLabel(但您没有考虑该客户在该部分中的索引是否匹配当前 indexPath.row 与否)。这意味着在您的代码中,每个单元格都将包含最后一个客户的 textLabel 和 detailTextLabel,其首字母与当前部分的首字母匹配。

于 2012-04-29T13:39:56.917 回答
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];
于 2012-04-29T13:50:43.557 回答
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;
}
于 2012-04-29T13:10:53.780 回答