iOS - tableView reloadData 非常奇怪的问题

标签 ios xcode tableview reloaddata

    - (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];”,一切都绝对正常,但如果我这样做,似乎单元格在那里但没有显示(单元格分隔符根据表格 View 的单元格数量消失有)

我需要重新加载数据,这样用户就不需要退出 View 并再次加载 View 来显示更新的信息。

有什么建议吗?

最佳答案

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

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

关于iOS - tableView reloadData 非常奇怪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11176923/

相关文章:

ios - 如何将事件处理程序 block 放在自定义类上

ios - 如何获取 TableView 中点击对象的索引?

listview - 对于 Flutter 中的大量对象,我应该使用 Table 还是 ListView?

JavaFX GUI - 为什么删除按钮功能不适用于 TableView?

ios - 检测是否已从集成应用商店 IOS 下载应用

ios - 通过 iPhone IOS 测试 React Native

objective-c - 将来自 AFHTTPRequestOperation 的响应传递给 UIWebview

swift - 在 MacOS 中禁用重复通知

ios - 有没有办法在进入前台时强制全局刷新 View ?

ios - 带有文本输入到 UITextField 的自定义键盘