セクションが 1 しかないレベルで tableview を定義するデモを作成しましたが、行はレベルに従って定義されます。cellforRowAtIndexPath レベルでカスタム セルを設定するにはどうすればよいですか。このコードは、CustomCell を使用せずに実行するとスムーズに動作します。しかし、特定のレベルにcustomCellを含めると。「[CustomCell txtAddress]セレクターとして認識されません」のようなエラーが発生します。これは、テーブルビューをスクロールしたときにのみ発生します。正確に言えば、アプリがクラッシュします。
私のコードは次のとおりです。
- (void)viewDidLoad
{
[super viewDidLoad];
arraylist = [[NSMutableArray alloc]init];
array5 = [[NSMutableArray alloc]init];
array1 = [[NSMutableArray alloc]init];
objects = @[@"Medical Services In Chicago",@"Medical Services On Campus"];
arrObjectValueChicago = @[@"Ronald McDonald® Children's Hospital of Loyola",@"Burn Centers",@"Gottlieb Hospitals"];
arrObjectValueCampus = @[@"Cardinal Bernardin Cancer Center1",@"Center for Heart & Vascular Medicine2"];
for (int i =0; i< [arrObjectValueCampus count]; i++) {
dictListCampus1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2",@"level",[arrObjectValueCampus objectAtIndex:i],@"name", nil];
[array5 addObject:dictListCampus1];
}
NSDictionary *dictListCampus = [[NSDictionary alloc]initWithObjectsAndKeys:@"Wellness Centers",@"name",@"1",@"level",array5,@"Objects",nil];
NSMutableArray *array6 = [[NSMutableArray alloc]initWithObjects:dictListCampus, nil];
array3 = [[NSMutableArray alloc]init ];
for(int i = 0; i<[arrObjectValueChicago count];i++){
dictList3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2",@"level", [arrObjectValueChicago objectAtIndex:i],@"name", nil];
[array3 addObject:dictList3];
}
NSDictionary *dictList2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Hospitals",@"name",@"1",@"level",array3,@"Objects",nil];
NSMutableArray *array2 = [[NSMutableArray alloc]initWithObjects:dictList2, nil];
for (int i =0; i<[objects count]; i++) {
if (i == 0) {
dictionary =[[NSDictionary alloc]initWithObjectsAndKeys:[objects objectAtIndex:0],@"name",@"0",@"level",array2,@"Objects", nil];
[array1 addObject:dictionary];
}
else if (i == 1)
{
dictionary =[[NSDictionary alloc]initWithObjectsAndKeys:[objects objectAtIndex:1],@"name",@"0",@"level",array6,@"Objects", nil];
[array1 addObject:dictionary];
}
}
dictList = [[NSDictionary alloc]initWithObjectsAndKeys:array1,@"Objects", nil];
arrayOriginal = [dictList valueForKey:@"Objects"];
[arraylist addObjectsFromArray:arrayOriginal];
}
#pragma mark - TableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arraylist count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger indentLevel = [[[arraylist objectAtIndex:indexPath.row]valueForKey:@"level"]intValue];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: [CustomCell reuseIdentifier]];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
cell = self.cells;
self.cells = nil;
}
if (indentLevel == 0) {
cell.lblHeader.text = [[arraylist objectAtIndex:indexPath.row]valueForKey:@"name"];
cell.lblSubHeader.hidden = YES;
cell.imgSubHeader.hidden = YES;
cell.txtAddress.hidden = YES;
cell.btnarrow.hidden= YES;
cell.btnCall.hidden = YES;
cell.btnCallNumber.hidden = YES;
cell.btnHome.hidden = YES;
cell.btnHomeAddress.hidden = YES;
cell.btnUrl.hidden = YES;
cell.imgline.hidden = YES;
return cell;
}
else if (indentLevel == 1) {
cell.lblSubHeader.text = [[arraylist objectAtIndex:indexPath.row]valueForKey:@"name"];
cell.lblHeader.hidden = YES;
cell.imgHeader.hidden = YES;
cell.txtAddress.hidden = YES;
cell.btnCall.hidden = YES;
cell.btnCallNumber.hidden = YES;
cell.btnHome.hidden = YES;
cell.btnHomeAddress.hidden = YES;
cell.btnUrl.hidden = YES;
cell.imgline.hidden = YES;
return cell;
}
else if (indentLevel == 2) {
cell.txtAddress.text = [[arraylist objectAtIndex:indexPath.row]valueForKey:@"name"];
cell.lblHeader.hidden = YES;
cell.btnarrow.hidden= YES;
cell.imgHeader.hidden = YES;
cell.lblSubHeader.hidden = YES;
cell.imgSubHeader.hidden = YES;
return cell;
}
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row]valueForKey:@"level"]intValue]] ;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tblist deselectRowAtIndexPath:indexPath animated:YES];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: [CustomCell reuseIdentifier]];
NSUInteger indentLevel = [[[arraylist objectAtIndex:indexPath.row]valueForKey:@"level"]intValue];
if (indentLevel == 1) {
[cell.btnarrow setImage:[UIImage imageNamed:@"dwn1_arow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_active.png" ]];
}
NSDictionary *d = [arraylist objectAtIndex:indexPath.row];
if ([d valueForKey:@"Objects"] ) {
NSArray *ar = [d valueForKey:@"Objects"];
BOOL isAlreadyInserted = NO;
for(NSDictionary *dInner in ar ){
NSInteger index=[arraylist indexOfObjectIdenticalTo:dInner];
isAlreadyInserted=(index>0 && index!=NSIntegerMax);
if(isAlreadyInserted) break;
}
if(isAlreadyInserted) {
[self miniMizeThisRows:ar];
} else {
NSUInteger count=indexPath.row+1;
NSMutableArray *arCells=[NSMutableArray array];
for(NSDictionary *dInner in ar ) {
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[arraylist insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationNone];
}
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger indentLevel = [[[arraylist objectAtIndex:indexPath.row]valueForKey:@"level"]intValue];
if (indentLevel == 0) {
return 40;
}
else if (indentLevel == 1)
{
return 25;
}
else if (indentLevel == 2)
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
CGSize maximumSize = CGSizeMake(300, 9999);
UIFont *myFont = [UIFont fontWithName:@"Arial" size:11.5];
CGSize myStringSize = [cell.txtAddress.text sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
if (myStringSize.height > 80) {
myStringSize.height = 50;
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
cell.txtAddress.scrollEnabled = YES;
}
else
{
cell.txtAddress.scrollEnabled = YES;
myStringSize.height = 40;
}
//NSLog(@"String Size :: %f", myStringSize.height);
return myStringSize.height+ 50;
}
else
return 40;
}
#pragma mark - TableAnimation
-(void)miniMizeThisRows:(NSArray*)ar
{
for(NSDictionary *dInner in ar ) {
NSUInteger indexToRemove=[arraylist indexOfObjectIdenticalTo:dInner];
NSArray *arInner=[dInner valueForKey:@"Objects"];
if(arInner && [arInner count]>0){
[self miniMizeThisRows:arInner];
}
if([arraylist indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
[arraylist removeObjectIdenticalTo:dInner];
[tblist deleteRowsAtIndexPaths:[NSArray arrayWithObject:
[NSIndexPath indexPathForRow:indexToRemove inSection:0]]
withRowAnimation:UITableViewRowAnimationNone];
}
}
}