0

グループ化されたテーブルビューでデータを表示しました。データは、XML解析からテーブルビューに表示されます。テーブルビューのセクションが2つあり、セクション1には3行、セクション2には2行があります。

  section 1 ->  3 Rows

  section 2 - > 2 Rows.

ここで、文字列のいずれかが空の場合は空のセルを削除する必要があることを確認したいので、いくつかの問題に直面しました。空のセルを削除すると、インデックス番号が変更されます。では、どうすればフィールドの誰かが空であるかを確認できますか?、空のフィールドの数が増えることがあるため、インデックスの位置が変更されます。それで、そのためのサンプルコードまたはリンクを送ってください。どうすればこれを達成できますか?

サンプルコード、

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section       
 {

 if (section == 0) {

    if([userEmail isEqualToString:@" "] || [phoneNumber isEqualToString:@" "] || [firstName isEqualToString:@" "])
    {
        return 2;

    } 

    else {

        return 3;

    }
}
if (section == 1) {

       if(![gradYear isEqualToString:@" "] || ![graduate isEqualToString:@" "]) {

    return 1;
}
    else
   {
        return 2;
     }


return 0;

}

私を助けてください!!!

ありがとう。

4

2 に答える 2

2

私の理解によると、データが空の行を追加したくないので、セクションと行についてテーブルビューに通知する前に、セクションデータを作成することをお勧めします。

だから、次のコードがあなたを助けるかもしれません...、私はあなたが「viewDidLoad」メソッドからメソッド「prepareSectionData」を呼び出して.hファイルでセクション配列を定義する必要があることをテストしました。

- (void) prepareSectionData {
 NSString *userEmail = @"";
 NSString *phoneNumber = @"";
 NSString *firstName = @"";

 NSString *gradYear = @"";
 NSString *graduate = @"";

 sectionOneArray = [[NSMutableArray alloc] init];
 [self isEmpty:userEmail]?:[sectionOneArray addObject:userEmail];
 [self isEmpty:phoneNumber]?:[sectionOneArray addObject:phoneNumber];
 [self isEmpty:firstName]?:[sectionOneArray addObject:firstName];

 sectionTwoArray = [[NSMutableArray alloc] init];
 [self isEmpty:gradYear]?:[sectionTwoArray addObject:gradYear];
 [self isEmpty:graduate]?:[sectionTwoArray addObject:graduate];
}

 -(BOOL) isEmpty :(NSString*)str{
 if(str == nil || [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0)
     return YES;
 return NO;
 }

  // Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0){
    return [sectionOneArray count];
} else if (section == 1) {
    return [sectionTwoArray count];
}
return 0;
}


// Customize the appearance of table view cells.

- (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];
}

// Configure the cell.
if(indexPath.section == 0){
    cell.textLabel.text = [sectionOneArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
    cell.textLabel.text = [sectionTwoArray objectAtIndex:indexPath.row];
}
return cell;
}
于 2011-01-25T12:59:11.223 に答える
0

@Pugal Devanええと、データを1つの配列に保持できますが、その場合の問題は、配列の境界を処理し、さまざまなセクションのインデックスを修正する必要があることです。各セクションのindexPath.rowはインデックス0から始まり、データが単一の配列にある場合は、行インデックスを自分で管理する必要があります。しかし、それでもそれを維持したい場合は、次のようにすることができます。

int sectionOneIndex = 0; 
int sectionTwoIndex = 3;
NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithObjects:@"email", @"Name", @"address", @"zipCode", @"country", nil];

上記の2つの整数は、さまざまなセクションの要素の開始位置を表します。セクションArrayの最初の3つのオブジェクトは、セクション1の一部であり、最後の2つのオブジェクトはセクション2の一部です。次に、正しい行数を返す必要があります。そのためにあなたは書くかもしれません:

if(section == 0) return [sectionArray count] - (sectionTwoIndex-1); //returns 3
else if(section == 1) return [sectionArray count] - sectionTwoIndex; //returns 2

または、カウントが静的である場合は、定数値を返すことができます。

また、配列から読み取るときに、このインデックスを行の値に追加するだけで、現在のセルの要素の正しい位置が返されます。

// Customize the appearance of table view cells.

- (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];
}

// Configure the cell.
if(indexPath.section == 0){
cell.textLabel.text = [sectionArray objectAtIndex:indexPath.row + sectionOneIndex];
} else if (indexPath.section == 1) {
cell.textLabel.text = [sectionArray objectAtIndex:indexPath.row + sectionTwoIndex];
}
return cell;
}
于 2011-02-01T07:24:19.073 に答える