私はiPhone開発の初心者で、これが私の最初の質問です。
UIScrollView に UITableView を表示していますが、データが静的ではなくサービスから取得されるため、tableView と scrollView の両方の高さを指定できませんでした。
tableViewのコンテンツ(高さ)に応じて動的に高さを設定するには、scrollViewが必要です。これを実行しましたが、最後のセルの完全なデータが表示されませんでした。
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,327,480) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.scrollEnabled = NO;
[testscroll addSubview:self.tableView];
[self.tableView reloadData];
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.contentSize.height);
float ftbl = self.tableView.frame.origin.y + self.tableView.contentSize.height + 150;
scrollView.contentSize=CGSizeMake(320, ftbl);
scrollView.scrollEnabled=YES;
テーブルビューの高さを計算しています:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView: self.tableView cellForRowAtIndexPath: indexPath];
NSString *city = lblcity.text;
UIFont *font = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize bounds = [city sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
NSString *state = lblstate.text;
UIFont *font1 = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize1 = CGSizeMake(280.0f, MAXFLOAT);
CGSize bounds1 = [state sizeWithFont:font1 constrainedToSize:constraintSize1 lineBreakMode:NSLineBreakByWordWrapping];
NSString *name = lblname.text;
UIFont *font2 = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize2 = CGSizeMake(280.0f, MAXFLOAT);
CGSize bounds2 = [name sizeWithFont:font2 constrainedToSize:constraintSize2 lineBreakMode:NSLineBreakByWordWrapping];
NSString *address = lbladdress.text;
UIFont *font3 = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize3 = CGSizeMake(280.0f, MAXFLOAT);
CGSize bounds3 = [address sizeWithFont:font5 constrainedToSize:constraintSize3 lineBreakMode:NSLineBreakByWordWrapping];
return (CGFloat) cell.bounds.size.height + bounds.height+bounds1.height+bounds2.height+bounds3.height;
}
UItableviewの高さに応じてscrollViewの高さを増減するにはどうすればよいですか?
編集:
-(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];
lblname = [[UILabel alloc] initWithFrame:CGRectZero];
lblname.tag = 111;
lblname.backgroundColor = [UIColor clearColor];
[lblname setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
[lblname setLineBreakMode:NSLineBreakByWordWrapping];
[cell addSubview:lblname];
lbladdress = [[UILabel alloc] initWithFrame:CGRectZero];
lbladdress.tag = 113;
lbladdress.backgroundColor = [UIColor clearColor];
[lbladdress setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
[lbladdress setLineBreakMode:NSLineBreakByWordWrapping];
[cell addSubview: lbladdress];
lblcity = [[UILabel alloc] initWithFrame:CGRectZero];
lblcity.tag = 115;
lblcity.backgroundColor = [UIColor clearColor];
[lblcity setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
[lblcity setLineBreakMode:NSLineBreakByWordWrapping];
[cell addSubview: lblcity];
lblstate=[[UILabel alloc] initWithFrame:CGRectZero];
lblstate.tag = 116;
lblstate.backgroundColor = [UIColor clearColor];
[lblstate setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
[lblstate setLineBreakMode:NSLineBreakByWordWrapping];
[cell addSubview: lblstate];
}
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
NSMutableDictionary *d =[[NSMutableDictionary alloc]initWithDictionary: [providerarray objectAtIndex:indexPath.row]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *name2 = [d objectForKey:@"Name"];
CGSize constraint1 = CGSizeMake(175, 2000.0f);
CGSize size1 = [name2 sizeWithFont: [UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];
lblname = (UILabel *)[cell viewWithTag:111];
lblname.text = [NSString stringWithFormat:@"%@",name2];
[lblname setNumberOfLines:0];
lblname.frame = CGRectMake(105,25, size1.width, size1.height);
NSString *lane2 = [d objectForKey:@"Address"];
CGSize constraint3 = CGSizeMake(175, 2000.0f);
CGSize size3 = [line2 sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint3 lineBreakMode:NSLineBreakByWordWrapping];
lbladdress = (UILabel *)[cell viewWithTag:113];
lbladdress.text = [NSString stringWithFormat:@"%@",lane2];
[lbladdress setNumberOfLines:0];
lbladdress.frame = CGRectMake(105,lblname.frame.size.height+lblname.frame.origin.y, size3.width, size3.height);
NSString *city2 =[d objectForKey:@"City"];
NSString *trimmedString = [city2 stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@", trimmedString);
CGSize constraint5 = CGSizeMake(175, 2000.0f);
CGSize size5 = [trimmedString sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint5 lineBreakMode:NSLineBreakByWordWrapping];
lblcity = (UILabel *)[cell viewWithTag:115];
lblcity.text = [NSString stringWithFormat:@"%@",trimmedString];
[lblcity setNumberOfLines:0];
lblcity.frame = CGRectMake(105,lbladdress.frame.size.height+lbladdress.frame.origin.y, 175, size5.height);
NSString *state1=[d objectForKey:@"State"];
CGSize constraint6=CGSizeMake(175,2000.0f);
CGSize size6=[state1 sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint6 lineBreakMode:NSLineBreakByWordWrapping];
state=(UILabel *)[cell viewWithTag:116];
[state setText:[NSString stringWithFormat:@"%@",state1]];
state.frame=CGRectMake(105, lblcity.frame.size.height+lblcity.frame.origin.y, size6.width, size6.height);
[state setTextAlignment:NSTextAlignmentLeft];
[state sizeToFit];
return cell;
}