【问题标题】:How to refresh the uitableivew with an alertview?如何使用警报视图刷新 uitableivew?
【发布时间】:2014-03-31 06:50:52
【问题描述】:

在.h文件中:

@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...");


    }

}

如何使用 UIAlertView 委托 刷新 UITableView?我已经尝试过reloadData:reloadRowsAtIndexPaths:,就像上面一样。但他们没有帮助。任何帮助将不胜感激。

【问题讨论】:

  • 您是否记录了 buttonIndex one 的任何语句?
  • 是的,没关系。
  • 能否提供alertView的代码?
  • 放置断点可能是您的 AlertView 委托未被调用。
  • [[NSOperationQueue mainQueue]addOperationWithBlock:^{self.table reloadData}];尝试在主线程中刷新表格视图

标签: ios objective-c uitableview


【解决方案1】:

有些时间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

【讨论】:

  • 他用过reloadData
  • 不,延迟执行选择器不起作用。但感谢您的尝试!
  • @tipsywacky - 请尝试更改afterDelay 就像 1 秒一样。 ?
  • withObject:self 到一个不带任何参数的方法,这是怎么回事?
【解决方案2】:

您应该只更新您的数据源并调用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];
    }
}

希望对你有帮助!

编辑

我有两个关于你的更新的 cmets:

  1. 您正在使用 listItems 填充您的 cell.textLabel.text 并使用 objectForKey word。尝试更新 listItems 集合中键 word 的值。因此,您的单元格事件将使用新值。

  2. 避免为您的 UITableView 使用相同的名称 tableView,因为它的事件使用此名称。我个人要么小心使用self 方法,要么使用不同的名称以避免出现类似实例名称的警告。

【讨论】:

  • 来自 Apple Docs 按钮索引从 0 开始,第一个按钮是 No 按钮。
  • 是的,我只是看到 OP 用 alertview 代码更新了问题。
  • IMO 最好使用 cancelButtonIndex: if (buttonIndex != [alertView cancelButtonIndex]) {
【解决方案3】:

来自Apple Docs不应在插入或删除行的方法中调用它,尤其是在通过调用 beginUpdates 和 endUpdates 实现的动画块中

您只能使用reloaddata,而不是beginUpdates

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

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

        [self.tableView reloadData];

    }

}

【讨论】:

    【解决方案4】:

    一次尝试这么多事情有时行不通。

    如果你有适当的委托和数据源的 tableview 连接,而不仅仅是简单的:[self.tableView reloadData];

    会起作用的。

    【讨论】:

      猜你喜欢
      • 2021-08-25
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 2014-12-04
      相关资源
      最近更新 更多