- 同じビュー (ScrollView) に 2 つの tableView を別々にロードしています。 ) コンテンツ。どこが間違っているのか理解できませんか?
これは私が取り組んでいるコードです:
- (void)viewDidLoad
{
self.tableView1=[[UITableView alloc] initWithFrame:CGRectMake(10,10,310,250) style:UITableViewStylePlain];
self.tableView1.delegate=self;
self.tableView1.dataSource=self;
self.tableView1.rowHeight=80;
self.tableView1.scrollEnabled = YES;
currenttableView=@"1";
[testscroll addSubview:self.tableView1];
[self performSelector:@selector(callWebserviceMethod) withObject:nil afterDelay:0];
}
-(void)callWebserviceMethod
{
self.tableView2=[[UITableView alloc] initWithFrame:CGRectMake(10,self.tableView1.frame.origin.y+self.tableView1.frame.size.height+6,310,230) style:UITableViewStylePlain];
self.tableView2.delegate=self;
self.tableView2.dataSource=self;
self.tableView2.rowHeight=80;
currenttableView=@"2";
self.tableView2.scrollEnabled = YES;
[testscroll addSubview:self.tableView2];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(currenttableView==@"1")
{
cell.textLabel.text = [[filtered objectAtIndex:indexPath.row] objectForKey:@"CompanyName"] ; //filtered is NSMutableArray
cell.textLabel.tag=3;
cell.textLabel.font=[UIFont boldSystemFontOfSize:12];
UIButton *showDirections = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showDirections.frame = CGRectMake(10.0f, 52.0f, 85.0f, 20.0f);
[showDirections setTitle:@"Direction" forState:UIControlStateNormal];
[showDirections setTag:indexPath.row];
[showDirections.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
showDirections.backgroundColor=[UIColor lightTextColor];
[ showDirections setBackgroundImage:[UIImage imageNamed:@"background-button.png"] forState:UIControlStateNormal];
showDirections.titleLabel.textColor=[UIColor whiteColor];
[cell addSubview:showDirections];
[showDirections addTarget:self action:@selector(showDirectionsfordC:)forControlEvents:UIControlEventTouchUpInside];
return cell;
}
if(currenttableView==@"2")
{
cell.textLabel.text = [[places objectAtIndex:indexPath.row] objectForKey:@"name"] ; //places is NSMutableArray
cell.textLabel.lineBreakMode=UILineBreakModeWordWrap;
cell.textLabel.numberOfLines=0;
cell.textLabel.font=[UIFont boldSystemFontOfSize:12];
cell.textLabel.tag=3;
UIButton *showDirections = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showDirections.frame = CGRectMake(10.0f, 52.0f, 85.0f, 20.0f);
[showDirections setTitle:@"Direction" forState:UIControlStateNormal];
[showDirections setTag:indexPath.row];
[showDirections.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
showDirections.backgroundColor=[UIColor lightTextColor];
[ showDirections setBackgroundImage:[UIImage imageNamed:@"background-button.png"] forState:UIControlStateNormal];
showDirections.titleLabel.textColor=[UIColor whiteColor];
[cell addSubview:showDirections];
[showDirections addTarget:self action:@selector(showDirections:)forControlEvents:UIControlEventTouchUpInside];
NSString *vicinity1 = [[places objectAtIndex:indexPath.row]objectForKey:@"vicinity"];
UILabel *vicinity = [[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320, 10)];
vicinity .font=[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:12];
[vicinity setTextAlignment:UITextAlignmentLeft];
vicinity.text=[NSString stringWithFormat:@"%@",vicinity1];
vicinity.tag=4;
[cell addSubview:vicinity ];
vicinity.hidden=YES;
[vicinity release];
return cell;
}
}
どこが間違っているのですか?
2.. tableView2 が空の場合、ScrollView は tableView1 のみでロードされます。その後、tableView1 をスクロールしようとすると、エラーが発生します: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
tableView2 のこの行で
cell.textLabel.text = [[places objectAtIndex:indexPath.row] objectForKey:@"name"] ;//error
どうすればうまくいくでしょうか?