2010-07-05 67 views
0

使用下面的代碼我已經能夠在沒有數據在一個UITableView顯示,顯示了一條短信:commitEditingStyle顯示文本沒有記錄後發現的缺失

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
// Return the number of rows in the section. 
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate]; 

if ([appDelegate.myDataArray count] == 0) 
{ 
    return 3; 
} 

return [appDelegate.myDataArray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


static NSString* myCellIdentifier = @"MyCell"; 

    myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate]; 

if ([appDelegate.myDataArray count] == 0) 
{ 
    if (indexPath.row == 2) 
    { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier]; // only reuse this type of cell here 

     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCellIdentifier] autorelease]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 
     cell.textLabel.text = (@"No Records Found"); 
     return cell; 
    } 
    else 
    { 
     // return a blank row for spacing 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier]; // only reuse this type of cell here 

     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCellIdentifier] autorelease]; 
     } 
     cell.textLabel.text = (@""); 
     return cell; 
    } 
} ........ 

我的問題是,我想有這種行爲的時候我一直在使用該方法刪除表中的記錄:

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath { 

目前,其結果是,當我刪除最後一個記錄崩潰: 」 ......包含在現有的部分中的行數更新後(3)必須等於包含在部分行的更新(1)前面的數字......」

的我怎麼能有文字顯示3行下(如上圖所示)後,我刪除最後一個記錄,而不會崩潰任何想法刪除?

謝謝。

回答

0

雖然我不會用同樣的方式來實現「無結果」的反饋,我認爲你的錯誤表示的過渡問題,當最後一個「真實」的行被刪除。

修復它的方法是告訴表,當最後一個被刪除時,您插入了3行。那或者強制在那一點重新加載表格視圖[self.tableView reloadData]以確保它不會與轉換混淆。

+0

您好,感謝您的答覆。我全都換了另一種方法來實現「無結果」。如果你有建議,我會在我的最後嘗試。它可能會讓一切變得更容易 – CraigH 2010-07-06 01:54:05

+0

我會添加一個覆蓋'UIView',其中包含「找不到記錄」標籤。您可以在IB中設計視圖並將其掛接到保留的「IBOutlet」屬性,或根據需要創建它。如果您使用的是「UITableViewController」的子類,則需要將tableView交換爲覆蓋視圖。如果沒有,您可以將疊加視圖添加到'self.view'。 – ohhorob 2010-07-06 16:39:27

+0

謝謝。這說得通。我使用覆蓋時,需要一個UIActivityIndi​​cator但是,是的,它是有道理在這種情況下使用它。 – CraigH 2010-07-06 20:24:18