【问题标题】:UITableView reloadData doesn't work on tap eventUITableView reloadData 对点击事件不起作用
【发布时间】:2015-07-24 05:37:39
【问题描述】:

我有一个动态创建的弹出视图,其中包含动态创建的元素:

UITapGestureRecognizer 添加到“发布”按钮,即UILabel。一旦点击“发布”,它就会从UITextView 获取文本并将其提交到后台服务器。之后我打电话给:

[self.tableView reloadData];

绝对没有任何反应,我尝试了很多解决方案,但似乎没有任何效果,我显然在这里遗漏了一些东西,请指教

编辑 这是tableView:cellForRowAtIndexPath:

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

//reuse the cell from the reuse pool
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];

//if there are no cells in the reuse pool then create a new cell
if (cell == nil) {

    //get the nib file from the main bundle
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

    //choose our cell from the array of nibs
    cell = [nib objectAtIndex:0];
}

//give the cell's message property a preferred max layout width of 300
cell.message.preferredMaxLayoutWidth = 300.0;

// Configure the cell with the data
[self configureCell:cell forRowAtIndexPath:indexPath];

return cell;}

还有configureCell:forRowAtIndexPath:

- (void)configureCell:(CustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

if ([cell isKindOfClass:[CustomCell class]]) {

    //get data from the messages array
    NSDictionary *data = [self.messages objectAtIndex:indexPath.row];

    //give the colors to the username and the venue
    cell.venue.textColor = [UIColor colorWithRed:119.0/255.0 green:119.0/255.0 blue:119.0/255.0 alpha:1.0];
    cell.username.textColor = [UIColor colorWithRed:51.0/255.0 green:102.0/255.0 blue:153.0/255.0 alpha:1.0];
    cell.time.textColor = [UIColor blackColor];


    //get the name of the venue
    NSString *venue = [[self.messages objectAtIndex:indexPath.row] objectForKey:@"venue"];

    //if the user didn't specify the venue
    if ([venue isEqualToString:@""]) {
        cell.venue.text = @"No Venue Specified"; //use the placeholder
    } else {
        cell.venue.text = venue;    //otherwise use the venue's name
    }

    //post message and the username
    cell.message.text = [data objectForKey:@"message"];
    cell.username.text = [data objectForKey:@"username"];
}}

【问题讨论】:

  • 你在后台进程完成后调用[self.tableView reloadData];吗?
  • 检查webservice的响应和你的tableview数组内容
  • 也许您正在后台调用服务,响应后您正在后台重新加载数据。您必须在主线程中重新加载表。
  • @Akhilrajtr 这是我用来保存在后台的 Parse sn-p,它发生在 UITapGestureRecognizer 的响应中:[messageObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { [self.tableView reloadData]; }];
  • @ChintaN-Maddy-Ramani 好吧,如果我异步调度到主队列它也什么都不做:/

标签: ios objective-c uitableview cocoa-touch reloaddata


【解决方案1】:

你必须设置你的tableview的delegatedatasource。 在您的 .h 文件中写入 <UITableViewDataSource,UITableViewDelegate>。 并在您的 .xib 文件中设置表的 delegatedatasource

【讨论】:

  • 好吧,它是UITableViewController 的子类,因此无需指定协议,但即使我将协议放入并执行:self.tableView.dataSource = selfself.tableView.delegate = self 无论如何也不起作用
【解决方案2】:

试试这个,

[messageObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
     //update the table data source array with new messageObject
     //then reload the tableview by
     dispatch_async(dispatch_get_main_queue(), ^{
         [self.tableView reloadData]; 
     });
}];

【讨论】:

  • 不幸的是,它不会重新加载表格视图
  • 您的 tableview 数据源数组在重新加载之前是否正在更新?我怀疑您没有使用新创建的messageObject 更新表数据源数组。
  • 好吧,我的数据源已使用来自服务器的数据进行更新:/ 在 viewWillAppear 我向服务器查询数据,然后我将数据分配给一个属性并使用该属性tableView: cellForRowAtIndexPath:,所以我猜你的问题的答案是“不”。
  • 理想情况下,在将 messageObject 保存到服务器后,您需要将该对象添加到数组中。那么只有在重新加载新对象时才会在 tableview 中添加或列出。
  • 你会怎么做?我只使用了配置单元格的数据源委托。
【解决方案3】:

如果你使用手势,请试试这个

tap.cancelsTouchesInView = NO;

可能对你有帮助

【讨论】:

    【解决方案4】:

    当您收到 POST 方法的响应时,请写 [self.tableView reloadData]; 可能会起作用。

    【讨论】:

    • 是的,我在收到服务器的响应后调用reloadData,什么都不做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2016-05-13
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2018-06-15
    相关资源
    最近更新 更多