gpt4 book ai didi

objective-c - UITableViewController 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-04 03:02:54 25 4
gpt4 key购买 nike

UITableViewController 在滚动或 viewDidAppear 时卡住:。我看到当问题开始时,应用程序开始不受控制地分配对象。我有以下 iOS 项目:基本上是从 CoreData 下载信息,但是当我去显示表格中的数据时,应用程序卡住(设备和模拟器) ).但是,如果我在下载的 NSLog 中显示排列,则没有错误。代码如下:

I see that when the problem starts, the app start to allocate objects uncontrollably.

The UITableViewController freezes when scrolling or when viewDidAppear:

TableViewController.h

@interface Set_ScheduleViewController : UITableViewController
{
STCore *core;
UILabel *label;
NSArray *objects;
}
@end

TableViewController.m:

@implementation Set_ScheduleViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
core = [[STCore alloc] init];
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)reloadData
{
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
objects = [core getAllClassesUsingSortDescriptions:@[sort]];
[[self tableView] reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
[self reloadData];
[super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [objects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 20)];
label.textColor = [UIColor colorWithHex:0x88B6DB];
label.font = [UIFont systemFontOfSize:12.0f];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentRight;
label.minimumFontSize = 12.0f;

tableView.separatorColor = [UIColor colorWithHex:0xDAE1E6];

cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithHex:0x393B40];
cell.textLabel.font = [UIFont boldSystemFontOfSize:16.0f];

cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.textColor = [UIColor colorWithHex:0x393B40];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
}

cell.textLabel.text = [[objects objectAtIndex:indexPath.row] name];
cell.detailTextLabel.text = [[objects objectAtIndex:indexPath.row] location];

STMultipleDate *dates = [STMultipleDate new];
for (Schedule *sch in [[objects objectAtIndex:indexPath.row] schedule]) {
STDate *date = [STDate new];
[date setWeekday:[[sch day] integerValue]];
[date setHour:[[sch hour] integerValue]];
[date setMinutes:[[sch minutes] integerValue]];

[dates addDate:date];
}

label.text = [STCore splitDates:dates];
cell.accessoryView = label;
return cell;
}


- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
if ([objects count] < 1) {
return NSLocalizedString(@"SETSCH_FOOTER_DEFAULT_404", @"");
}
return nil;
}

STCore.m

- (NSArray *)getAllClassesUsingSortDescriptions:(NSArray *)descs
{
if (![self isReady]) {
return @[];
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Classes" inManagedObjectContext:_context];

[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:descs];

NSError *error;
NSArray *returned = [_context executeFetchRequest:fetchRequest error:&error];

if (error) {
[_delegate core:self didRecivedAnError:error];
}

return returned;
}

最佳答案

在您的 viewDidLoad 中,您使用 [[core context] lock]; 锁定上下文,这导致您的 getAllClassesUsingSortDescriptions 等待以清除锁定。取下锁。

关于objective-c - UITableViewController 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15736686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com