空のフィールドが来たら、iPhoneのグループ化されたテーブルビューの行を削除しましたか?

StackOverflow https://stackoverflow.com/questions/4793198

  •  24-10-2019
  •  | 
  •  

質問

グループ化されたテーブルビューにデータを表示しました。データは、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;

}

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

ありがとう。

役に立ちましたか?

解決

私の理解によると、データが空になっている行を追加したくないので、セクションと行についてテーブルビューを伝える前に、セクションデータを実行する必要があることを示唆しています。

したがって、コードに従うことができるかもしれません...、私はそれをテストしました。「ViewDidload」メソッドから「preatecectionData」を呼び出して.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;
}

他のヒント

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

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

上記の2つの整数は、異なるセクションの要素の開始位置を表します。セクション配列の最初の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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top