当空字段到来时,删除iPhone中的Grouped Table视图中的行?

时间:2011-01-25 11:56:59

标签: iphone grouped-table

我在分组表视图中显示了数据。数据显示在XML解析的表视图中。我有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;

}

请帮帮我!!!

感谢。

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

@Pugal Devan, 好吧,您可以将数据保存在一个数组中,但在这种情况下的问题是,您必须处理数组边界并更正不同部分的索引。对于每个部分,indexPath.row将从索引0开始,如果您的数据是单个数组,则必须由您自己管理行索引。但是如果你想保留它,你可以这样做:

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

以上两个整数表示不同部分的元素的起始位置。 Section Array中的前3个对象是Section One的一部分,最后两个对象是Section 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;
}