2011-01-25 68 views
0

我已經在分組表格視圖中顯示數據。數據從XML解析顯示在表格視圖中。我有2節表視圖,第一節有三排,第二節有兩排。當空字段到來時,刪除iPhone中分組表格視圖中的行?

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

根據我的理解,你不想添加數據爲空的行,所以生病建議你應該在告訴表視圖關於部分和行之前進行節數據。

所以,下面的代碼可以幫助你......我測試過了,你只需要從「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; 
} 
+0

順便說一句,這個代碼將返回0行,因爲所有的變量現在都是空的,只需填寫任何字符串和看到的結果... – Ansari 2011-01-25 13:00:41

0

@Pugal拆箱, 那麼,你可以保持數據在一個陣列,但在這種情況下,問題是,你要照顧數組邊界和正確索引的不同部分。對於每個部分,indexPath.row將從索引0開始,並且如果數據在單個數組中,則必須由您自己管理行索引。但仍然如果你想保留它,你可以這樣做:

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

以上兩個整數表示您的不同部分的元素的起始位置。 Array部分的前3個對象是部分1的一部分,最後兩個對象是部分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; 
}