gpt4 book ai didi

ios - UITableViewController 中的 UITableView

转载 作者:行者123 更新时间:2023-11-28 22:17:45 24 4
gpt4 key购买 nike

我很难将一个 UITableViewController 与另一个 UITableView 一起使用。众所周知,UITableViewController 有自己的UITableView。我使用 UITableView 来显示细节;每个 UITableViewCell 都有对应的字段。所以我用一个唯一的标识符命名每个单元格。但是我需要另一个包含动态原型(prototype)单元格的 UITableView,这个 UITableView 的 字段与预制的 UITableView 不同,行数取决于数组计数。这是我的代码:

@interface CourseJobTVC ()
{ sqlite3 *_db;}

@property (weak, nonatomic) IBOutlet UITableView *cTableLabel;

@end

@implementation CourseJobTVC
@synthesize jDetails=_jDetails;

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

- (void)viewDidLoad
{
[super viewDidLoad];

_jDetails = (Jobs *)self.jDetails;

NSString *title1 = _jDetails.jName;
self.navigationItem.title = title1;
[self course];
}


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

-(NSArray *)course;
{
NSString *sqLiteDb = [[NSBundle mainBundle]pathForResource:@"CourseFindr" ofType:@"sqlite"];
sqlite3_stmt *statement;
NSMutableArray *retrieve = [[NSMutableArray alloc] init];
if (sqlite3_open([sqLiteDb UTF8String], &_db) == SQLITE_OK)
{
NSString *query= [NSString stringWithFormat:@"SELECT course. * FROM course INNER JOIN jobsCourse ON jobsCourse.courseID = course.cID WHERE jobsCourse.jobID = %d", _jDetails.jID];

if (sqlite3_prepare_v2(_db, [query UTF8String], -1, &statement, nil) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
int _cID = sqlite3_column_int(statement, 0);
char *cNameChars = (char *) sqlite3_column_text(statement,1);
char *cDescChars = (char *) sqlite3_column_text(statement, 2);
char *cSchoolChars = (char *) sqlite3_column_text (statement, 3);
char *cProgramChars = (char *) sqlite3_column_text(statement, 4);
NSString *_cName =cNameChars?[[NSString alloc]initWithUTF8String:cNameChars]:@"";
NSString *_cDesc = cDescChars?[[NSString alloc]initWithUTF8String:cDescChars]:@"";
NSString *_cSchool = cSchoolChars?[[NSString alloc]initWithUTF8String:cSchoolChars]:@"";
NSString *_cProgram = cProgramChars?[[NSString alloc]initWithUTF8String:cProgramChars]:@"";
Course *courses = [[Course alloc]
initWithCID:_cID
cName:_cName
cDesc:_cDesc
cSchool:_cSchool
cProgram:_cProgram];
[retrieve addObject:courses];
}
sqlite3_finalize(statement);
}

}
return retrieve;
}



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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView == self.cTableLabel) {NSLog(@"Array count: %d", [self.course count]);
return [self.course count];}

return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell ;
if(tableView != self.cTableLabel){
//jobName
cell = [self.tableView dequeueReusableCellWithIdentifier:@"jNameCell"];
if(cell ==nil)
{ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"jNameCell"];
cell.textLabel.text =_jDetails.jName;}

//jobEarnings
cell =[self.tableView dequeueReusableCellWithIdentifier:@"jEarningsCell"];
if(cell ==nil)
{cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"jEarningsCell"];
cell.detailTextLabel.text = _jDetails.jEarnings;}

//jobDesc
cell =[self.tableView dequeueReusableCellWithIdentifier:@"jDescCell"];
if(cell == nil)
{ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"jDescLabel"];
cell.detailTextLabel.text =_jDetails.jDesc;}
return cell;}

else if(tableView == self.cTableLabel){

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"showCDetails"];
Course *courses = [self.course objectAtIndex:indexPath.row];
cell.textLabel.text =courses.cName;
cell.detailTextLabel.text =courses.cSchool;
return cell;
}
return 0;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier]isEqualToString:@"showCourseDetails"]) {
CourseTVC *courseTVC = [segue destinationViewController];

courseTVC.cDetails = [self.course objectAtIndex:[self.cTableLabel indexPathForSelectedRow].row];
}
}


@end

我的错误是,我无法从预制的 UITableView 加载详细信息。当数组计数超过 2 时,其他 UITableView 崩溃。我我已经尝试过将 UIViewControllerUITableView 结合使用,但是当详细信息超出页面时,我无法让它滚动。而且它看起来非常杂乱无章。我想用 2 个 UITableViews 尝试 UIViewController 但我的代码确实有问题...请帮助。

崩溃报告:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 3]'
*** First throw call stack:
(
0 CoreFoundation 0x018325e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x015b58b6 objc_exception_throw + 44
2 CoreFoundation 0x017e69c2 -[__NSArrayI objectAtIndex:] + 210
3 UIKit 0x0076415e -[UITableViewDataSource tableView:heightForRowAtIndexPath:] + 127
4 UIKit 0x004f5e67 -[UITableViewController tableView:heightForRowAtIndexPath:] + 76
5 UIKit 0x0048cb4b __66-[UISectionRowData refreshWithSection:tableView:tableViewRowData:]_block_invoke + 462
6 UIKit 0x0048c450 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 4190
7 UIKit 0x0048f6bd -[UITableViewRowData numberOfRows] + 98
8 UIKit 0x00317db2 -[UITableView noteNumberOfRowsChanged] + 120
9 UIKit 0x0031775f -[UITableView reloadData] + 814
10 UIKit 0x0031b1c3 -[UITableView _reloadDataIfNeeded] + 65
11 UIKit 0x0031fde4 -[UITableView layoutSubviews] + 36
12 UIKit 0x002a4267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
13 libobjc.A.dylib 0x015c781f -[NSObject performSelector:withObject:] + 70
14 QuartzCore 0x03bf02ea -[CALayer layoutSublayers] + 148
15 QuartzCore 0x03be40d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
16 QuartzCore 0x03be3f40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
17 QuartzCore 0x03b4bae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
18 QuartzCore 0x03b4ce71 _ZN2CA11Transaction6commitEv + 393
19 QuartzCore 0x03b4d544 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
20 CoreFoundation 0x017fa4ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
21 CoreFoundation 0x017fa41f __CFRunLoopDoObservers + 399
22 CoreFoundation 0x017d8344 __CFRunLoopRun + 1076
23 CoreFoundation 0x017d7ac3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x017d78db CFRunLoopRunInMode + 123
25 GraphicsServices 0x037879e2 GSEventRunModal + 192
26 GraphicsServices 0x03787809 GSEventRun + 104
27 UIKit 0x00239d3b UIApplicationMain + 1225
28 CourseFindr 0x0000768d main + 141
29 libdyld.dylib 0x01e6e725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

因为您要混合表格 View (尤其是带有原型(prototype)单元格的表格 View ),所以事情会变得有点棘手。

为了简化您的代码,我建议将它们创建为单独的 UIViewController 子类,然后将一个作为 subview Controller 添加到另一个,这样它们就会有自己的委托(delegate),并且不会混淆上。

Apple 对此有一篇很好的文章: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

关于ios - UITableViewController 中的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21204368/

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