为什么向 NSArray 添加(通过输入代码)object 会冻结我的应用程序?
Why adding (by typing in code) an object to NSArray freezes my app?
我走投无路了。我有 UITableViewCell class 在我的应用程序中用作 "Settings" 选项卡。单元格的创建和计数如下:
- (void)viewDidLoad
{
[super viewDidLoad];
self.refreshControl = nil;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.scrollEnabled = NO;
(...) 注意标题 4 有注释
NSArray* titles = @[ @"Title1", @"Title2", @"Title3"/*,@"Title4"*/];
for (int i = 0; i < titles.count; i++)
{
SettingsTableCell* cell = [[SettingsTableCell alloc] initWithFrame: CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height / titles.count)];
cell.textLabel.text = [titles objectAtIndex:i];
cell.textLabel.numberOfLines = -1;
cell.backgroundColor = i % 2 == 0 ? [Colors colorForType:kLegiaColorWhite] : [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [Fonts fontWithSize: IPHONE_IPAD(18.0, 24.0)];
cell.accessoryView = i == 0 ? calendarSwitch : (i == 1 ? scheduleFilterSwitch : wifiSwitch);
[cells addObject:cell];
}
}
我认为下面的一切都是正确的
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [cells count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.tableView.frame.size.height / [self tableView:tableView numberOfRowsInSection:indexPath.section];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [cells objectAtIndex:indexPath.row];
}
请注意,当我取消注释数组中的第四个标题时会出现问题。
当我进入“设置”选项卡时,它开始自行循环(我检查了调试器)并且应用程序冻结而不是崩溃。有了三个标题,它就像一个魅力。你能帮我找出原因吗?
编辑:
根据@Avi 的建议,我在heightForRowAtIndexPath
上设置了断点,它在这个特定的方法上循环。在调试器中我有这个:
在研究和减少 *titles
数组中的项(它适用于 1 项和 2 项)之后,我注意到每个单元都有自己的 UISwitch,而第 4 个没有,因为不需要.由于此错误,布局子视图无法完成。
所以我更改了以下行:
cell.accessoryView = i == 0 ? calendarSwitch : (i == 1 ? scheduleFilterSwitch : wifiSwitch);
那个
cell.accessoryView = i == 0 ? calendarSwitch : (i == 1 ? scheduleFilterSwitch : i == 2 ? wifiSwitch : nil);
我工作了!
求助各位大侠!
我走投无路了。我有 UITableViewCell class 在我的应用程序中用作 "Settings" 选项卡。单元格的创建和计数如下:
- (void)viewDidLoad
{
[super viewDidLoad];
self.refreshControl = nil;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.scrollEnabled = NO;
(...) 注意标题 4 有注释
NSArray* titles = @[ @"Title1", @"Title2", @"Title3"/*,@"Title4"*/];
for (int i = 0; i < titles.count; i++)
{
SettingsTableCell* cell = [[SettingsTableCell alloc] initWithFrame: CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height / titles.count)];
cell.textLabel.text = [titles objectAtIndex:i];
cell.textLabel.numberOfLines = -1;
cell.backgroundColor = i % 2 == 0 ? [Colors colorForType:kLegiaColorWhite] : [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [Fonts fontWithSize: IPHONE_IPAD(18.0, 24.0)];
cell.accessoryView = i == 0 ? calendarSwitch : (i == 1 ? scheduleFilterSwitch : wifiSwitch);
[cells addObject:cell];
}
}
我认为下面的一切都是正确的
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [cells count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.tableView.frame.size.height / [self tableView:tableView numberOfRowsInSection:indexPath.section];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [cells objectAtIndex:indexPath.row];
}
请注意,当我取消注释数组中的第四个标题时会出现问题。 当我进入“设置”选项卡时,它开始自行循环(我检查了调试器)并且应用程序冻结而不是崩溃。有了三个标题,它就像一个魅力。你能帮我找出原因吗?
编辑:
根据@Avi 的建议,我在heightForRowAtIndexPath
上设置了断点,它在这个特定的方法上循环。在调试器中我有这个:
在研究和减少 *titles
数组中的项(它适用于 1 项和 2 项)之后,我注意到每个单元都有自己的 UISwitch,而第 4 个没有,因为不需要.由于此错误,布局子视图无法完成。
所以我更改了以下行:
cell.accessoryView = i == 0 ? calendarSwitch : (i == 1 ? scheduleFilterSwitch : wifiSwitch);
那个
cell.accessoryView = i == 0 ? calendarSwitch : (i == 1 ? scheduleFilterSwitch : i == 2 ? wifiSwitch : nil);
我工作了! 求助各位大侠!