2014-03-31 64 views
1

在.h文件:如何使用alertview刷新uitableivew?

@property (weak, nonatomic) IBOutlet UITableView *tableView; 

在.m文件:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"RememberedTableListCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    cell.textLabel.text = [[listItems objectAtIndex:indexPath.row] objectForKey:@"word"]; 
    return cell; 
} 

- (IBAction)resetList:(UIBarButtonItem *)sender { 

    UIAlertView *resetAlert = [[UIAlertView alloc] initWithTitle:@"Reset" message:@"Are you sure to reset the list?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
    [resetAlert show]; 

} 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    // the user clicked Yes 
    if (buttonIndex == 1) { 
     // reset the list 

     //reload table data 
     NSLog(@"button yes is clicked"); 

     for (NSMutableDictionary*l in listAll) { 
      [l setValue:@"0" forKey:@"remembered"]; 
     } 

     [self.tableView reloadData]; 
     //[[NSOperationQueue mainQueue]addOperationWithBlock:^{[self.tvRemembered reloadData];}]; 

     //[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone]; 


     NSLog(@"refreshing..."); 


    } 

} 

如何刷新的UITableViewUIAlertView中代表?我已經嘗試reloadData:reloadRowsAtIndexPaths:像上面那樣。但他們沒有幫助。任何幫助將不勝感激。

+0

您是否記錄過buttonIndex之一的任何語句? –

+0

是的,我沒有,沒關係。 – tipsywacky

+0

你能提供'alertView'的代碼嗎? –

回答

2

一段時間reloadData沒有工作,所以你需要reloadData隨着時間這樣像一些延遲,

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    // the user clicked Yes 
    if (buttonIndex == 1) { 
     [self performSelector:@selector(reloadTableData) withObject:self afterDelay:0.20]; // set afterDelay as per your requirement. 
    } 
} 

而且在方法

-(void)reloadTableData 
{ 
    [self.tableView reloadData]; 
} 

欲瞭解更多信息

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/reloadData

+2

他已經使用'reloadData ' –

+0

@HimanshuJoshi - 檢查我編輯的.. \ – iPatel

+0

不行,延遲執行選擇器不起作用,但是謝謝你試用! – tipsywacky

2

你應該st更新你的數據源並且調用reloadData應該可以工作。

(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    // the user clicked Yes 
    if (buttonIndex == 1) 
    { 
     // TODO : Update your data source (most likely array) here. 

     NSLog(@"refreshing..."); 
     [self.tableView reloadData]; 
    } 
} 

希望它有幫助!

編輯

我對你的更新兩點意見:

  1. 您正在使用listItems來填充你的cell.textLabel.text和objectForKey word使用。嘗試更新listItems集合中密鑰word的值。所以新的值被你的單元格事件使用。

  2. 避免爲您的UITableView使用相同的名稱tableView,因爲此名稱由其事件使用。我個人要麼注意使用self方法,要麼使用不同的名稱來避免類似實例名稱的警告。

+1

From Apple Docs *按鈕索引從0開始*第一個按鈕是** No **按鈕。 –

+0

是的,我剛剛看到OP updat用alertview代碼編輯問題。 – NeverHopeless

+2

IMO它會更好地使用cancelButtonIndex:if(buttonIndex!= [alertView cancelButtonIndex]){ – Simon

0

一次嘗試這麼多事情不工作有時。

如果你的tableview的代表&數據源的不僅僅是簡單的正確連接:[self.tableView reloadData];

會工作。

1

Apple Docs它不應該被稱爲在插入或刪除行的方法,尤其是在與調用beginUpdates和endUpdates

實現的動畫塊你必須只,而不是使用reloaddatabeginUpdates

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    // the user clicked Yes 
    if (buttonIndex == 1) { 

     [self.tableView reloadData]; 

    } 

}