0

を設定する際に問題が発生しUITableViewます。私はと持ってNSMutableArraycustomersます。次のようになります。

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."

だから私はオブジェクトの顧客を持っています。それは私を各顧客に分けます。そして、私は各オブジェクトにいくつかのキーを持っています。2番目の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

これを試してください(このコードをコピーして貼り付けてください):

- (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 に答える
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 に答える