【问题标题】:iOS - Very weird issue with tableView realodDataiOS - tableView reloadData 的非常奇怪的问题
【发布时间】:2012-06-24 11:05:50
【问题描述】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }



        [cell setBackgroundColor:[UIColor lightGrayColor]];
        cell.textLabel.textColor = [UIColor lightTextColor];
        cell.textLabel.backgroundColor = nil;
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.detailTextLabel.backgroundColor = nil; 

  // Get list of local notifications
        NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
        UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];

        // Display notification info
        [cell.textLabel setText:notif.alertBody];

        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"MMM dd 'at' HH:mm"];

        NSString *dateString = [format stringFromDate:notif.fireDate];

        NSString *textData = [[NSString alloc]initWithFormat:@"%@",dateString];

        [cell.detailTextLabel setText:textData];

        //[notif.fireDate description]



        [self.tableView reloadData];
        return cell;

    }

如果我不写“[self.tableView reloadData];”,一切都会变得绝对正常,但如果我这样做,似乎单元格在那里但它没有被显示(单元格分隔符根据表格视图的单元格数消失有)

我需要重新加载数据,这样用户就不需要离开视图并再次加载视图来显示更新的信息。

有什么建议吗?

【问题讨论】:

  • 正如 Omar 所说,这是重新加载数据的错误位置。做一个newItem 方法,然后在那里做一个[tableView reloadData]。或者当您删除/清除项目时。
  • 你能举个例子吗?
  • reloadData 仅用于告诉表视图应从您的控制器重新加载数据(即,将调用您在上面发布的一些委托方法)。如果您还没有,请查看this

标签: ios xcode tableview reloaddata


【解决方案1】:

您不需要在cellForRowAtIndexPath 中添加[self.tableView reloadData];,这是绝对错误的,因为调用reloadData 将再次调用cellForRowAtIndexPath,正如您所经历的那样会产生非常奇怪的问题

一般来说,当您想更改UITableView 的全部或部分内容时,您通常会调用[self.tableView reloadData];

【讨论】:

  • 我还能把重载数据放在哪里?
  • 例如,在按钮的 touchUpInside 或 viewWillAppear 中
【解决方案2】:

创建一个函数来检查是否有新数据可用(可能实现一个刷新按钮/拉动刷新)。如果有新数据可用,请致电[self.tableView reloadData];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多