0
我無法設置視圖控制器&表視圖的幀大小。這是我的看法層次結構無法在iphone中使用xcode 4設置表格視圖和視圖控制器的幀大小
內容畫面添加爲滾動視圖子視圖的圖像。我的內容視圖包含標籤,以編程方式添加的按鈕&文本視圖。
但是根據我下面列出的代碼,我無法設置視圖控制器框架的表視圖框架&。我的表格視圖是框架設置不正確&這就是爲什麼我必須向上/向下滾動以查看我的表格單元格內容。所以我有2個滾動條,即1表示滾動視圖& 1。
這裏是我的代碼
#define TABLE_CELL_HEIGHT 200.0f
#define ADD_VIEW_HEIGHT 30.0f
#define FONT_SIZE 24.0f
#define CELL_CONTENT_WIDTH 720.0f
#define CELL_CONTENT_MARGIN 50.0f
#define SectionHeight 50.0f
#define LEFT_MARGIN 25
#define RIGHT_MARGIN 25
#define TOP_MARGIN 35
#define BOTTOM_MARGIN 50
#define BOTTOM_FOOTER_MARGIN 32
#define DOC_WIDTH 768
#define DOC_HEIGHT 910
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"In viewDidLoad of PostDetailsViewController");
self.navigationItem.title = [post Title];
[self createUserInterface];
[self createCommentUserInterface];
self.contentView.backgroundColor = SET_BACKGROUND_IMAGE;
self.view.backgroundColor = SET_BACKGROUND_IMAGE;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self displayCommentsForPhotoID:[post ID]];
[self.commentsTableView reloadData];
CGRect frame = self.commentsTableView.frame;
frame.size.height = TABLE_CELL_HEIGHT*[commentsArray count];
self.commentsTableView.frame = frame;
CGRect viewFrame = self.contentView.frame;
viewFrame.size.height = viewFrame.size.height + (TABLE_CELL_HEIGHT*[commentsArray count]) + ADD_VIEW_HEIGHT;
self.contentView.frame = viewFrame;
[self.scrollView addSubview:self.contentView];
self.scrollView.contentSize = self.contentView.bounds.size;
}
- (void)createUserInterface {
lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 30.f)];
[lblTitle setBackgroundColor:[UIColor clearColor]];
lblTitle.text = @"Is This Fashion Post";
[self.contentView addSubview:lblTitle];
scrollViewForImage = [[UIScrollView alloc]initWithFrame:CGRectMake(20.0f, 60.0f, 280.0f, 200.0f)];
scrollViewForImage.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
scrollViewForImage.maximumZoomScale = 4.0;
scrollViewForImage.minimumZoomScale = 0.75;
scrollViewForImage.clipsToBounds = YES;
scrollViewForImage.delegate = self;
scrollViewForImage.showsHorizontalScrollIndicator = NO;
scrollViewForImage.showsVerticalScrollIndicator = NO;
//scrollViewForImage.contentMode = UIViewContentModeCenter;
scrollViewForImage.bouncesZoom = NO;
scrollViewForImage.bounces = NO;
//scrollViewForImage.autoresizesSubviews = YES;
[self.contentView addSubview:scrollViewForImage];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 200.0f)];
[self.scrollViewForImage addSubview:imageView];
lblBy = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 270.0f, 30.0f, 20.0f)];
[lblBy setBackgroundColor:[UIColor clearColor]];
lblBy.text = @"By:";
[self.contentView addSubview:lblBy];
lblUploaderName = [[UILabel alloc]initWithFrame:CGRectMake(60.0f, 270.0f, 200.0f, 20.0f)];
[lblUploaderName setBackgroundColor:[UIColor clearColor]];
[lblUploaderName setTextColor:[UIColor blueColor]];
[lblUploaderName setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showUserProfile)];
[lblUploaderName addGestureRecognizer:tapRecognizer];
[self.contentView addSubview:lblUploaderName];
lblOn = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 300.0f, 20.0f, 20.0f)];
[lblOn setBackgroundColor:[UIColor clearColor]];
lblOn.text = @"on";
[self.contentView addSubview:lblOn];
[lblOn release];
lblDateTime = [[UILabel alloc]initWithFrame:CGRectMake(45.0f, 300.0f, 255.0f, 20.0f)];
[lblDateTime setBackgroundColor:[UIColor clearColor]];
//lblDateTime.text = @"9/11/11 11:09 AM";
[self.contentView addSubview:lblDateTime];
//txtViewComments = [[UITextView alloc]initWithFrame:CGRectMake(12.0f, 330.0f, 280.0f, 70.0f)];
txtViewComments = [[UITextView alloc]initWithFrame:CGRectMake(12.0f, 325.0f, 280.0f, 70.0f)];
//[txtViewComments setBackgroundColor:[UIColor greenColor]];
txtViewComments.backgroundColor=[UIColor clearColor];
[txtViewComments setEditable:NO];
//txtViewComments.text = @"Toronto Film Festival, image from InStyle.com This is a comment on Is This Fashion";
[txtViewComments setFont:[UIFont fontWithName:@"Helvetica" size:17.0f]];
[self.contentView addSubview:txtViewComments];
[txtViewComments setText:[post Comment]];
CGRect frame = txtViewComments.frame;
frame.size.height = txtViewComments.contentSize.height;
txtViewComments.frame = frame;
//newHeight = frame.size.height + 330.0f + 20.0f;
newHeight = frame.size.height + 330.0f + 5.0f;
NSLog(@"newHeight => %f",newHeight);
//btnYes = [[UIButton alloc]initWithFrame:CGRectMake(20.0f, 410.0f, 60.0f, 50.0f)];
btnYes = [[UIButton alloc]initWithFrame:CGRectMake(20.0f, newHeight, 60.0f, 50.0f)];
[btnYes setBackgroundImage:[UIImage imageNamed:@"yes_icon.png"] forState:UIControlStateNormal];
[btnYes setBackgroundImage:[UIImage imageNamed:@"yes_icon.png"] forState:UIControlEventTouchUpInside];
[btnYes addTarget:self action:@selector(btnYesPressed)forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:btnYes];
//***lblYesPercentage = [[UILabel alloc]initWithFrame:CGRectMake(80.0f, 420.0f, 50.0f, 40.0f)];
//lblYesPercentage = [[UILabel alloc]initWithFrame:CGRectMake(80.0f, 420.0f, 70.0f, 40.0f)];
lblYesPercentage = [[UILabel alloc]initWithFrame:CGRectMake(80.0f, newHeight, 70.0f, 40.0f)];
[lblYesPercentage setBackgroundColor:[UIColor clearColor]];
[lblYesPercentage setFont:[UIFont fontWithName:@"Helvetica" size:24.0f]];
//[lblYesPercentage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"lblYesPercentage.png"]]];
[self.contentView addSubview:lblYesPercentage];
//**btnNo = [[UIButton alloc]initWithFrame:CGRectMake(150.0f, 410.0f, 50.0f, 50.0f)];
//btnNo = [[UIButton alloc]initWithFrame:CGRectMake(150.0f, 410.0f, 60.0f, 50.0f)];
btnNo = [[UIButton alloc]initWithFrame:CGRectMake(150.0f, newHeight, 60.0f, 50.0f)];
[btnNo setBackgroundImage:[UIImage imageNamed:@"no_icon.png"] forState:UIControlStateNormal];
[btnNo setBackgroundImage:[UIImage imageNamed:@"no_icon.png"] forState:UIControlEventTouchUpInside];
[btnNo addTarget:self action:@selector(btnNoPressed) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:btnNo];
//**lblNoPercentage = [[UILabel alloc]initWithFrame:CGRectMake(210.0f, 420.0f, 50.0f, 40.0f)];
//lblNoPercentage = [[UILabel alloc]initWithFrame:CGRectMake(220.0f, 420.0f, 70.0f, 40.0f)];
lblNoPercentage = [[UILabel alloc]initWithFrame:CGRectMake(220.0f, newHeight, 70.0f, 40.0f)];
[lblNoPercentage setBackgroundColor:[UIColor clearColor]];
[lblNoPercentage setFont:[UIFont fontWithName:@"Helvetica" size:24.0f]];
//[lblNoPercentage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"lblNoPercentage.png"]]];
[self.contentView addSubview:lblNoPercentage];
//newHeight = newHeight + 70;
newHeight = newHeight + 65;
//lblTags = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 480.0f, 50.0f, 20.0f)];
lblTags = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, newHeight, 50.0f, 20.0f)];
[lblTags setBackgroundColor:[UIColor clearColor]];
lblTags.text = @"Tags:";
[self.contentView addSubview:lblTags];
//newHeight = newHeight - 10;
newHeight = newHeight - 10;
//txtVwTagsValue = [[UITextView alloc]initWithFrame:CGRectMake(65.0f, 470.0f, 220.0f, 90.f)];
txtVwTagsValue = [[UITextView alloc]initWithFrame:CGRectMake(65.0f, newHeight, 220.0f, 90.f)];
//[txtVwTagsValue setBackgroundColor:[UIColor greenColor]];
[txtVwTagsValue setBackgroundColor:[UIColor clearColor]];
[txtVwTagsValue setEditable:NO];
[txtVwTagsValue setFont:[UIFont fontWithName:@"Helvetica" size:17.0f]];
[self.contentView addSubview:txtVwTagsValue];
[txtVwTagsValue setText:[post Tags]];
CGRect frame1 = txtVwTagsValue.frame;
frame1.size.height = txtVwTagsValue.contentSize.height;
txtVwTagsValue.frame = frame1;
[lblTitle setText:[post Title]];
//[btnUploaderName setTitle:[post Username] forState:UIControlStateNormal];
[lblUploaderName setText:[post Username]];
[lblDateTime setText:[post DateAdded]];
//[lblDateTime setText:[self returnDate:[post DateAdded]]];
//[txtViewComments setText:[post Comment]];
[lblYesPercentage setText:[post VoteYes]];
[lblNoPercentage setText:[post VoteNo]];
//[txtVwTagsValue setText:[post Tags]];
// Format the filename
NSString *formattedFilename = [[post Filename] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
// display image
NSString *url= [NSString stringWithFormat:@"%@%@",PHOTOURL,formattedFilename];
NSLog(@"url %@",url);
if (url !=nil) {
// Here we use the new provided setImageWithURL: method to load the web image
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImageWithURL:[NSURL URLWithString:url]
placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];
}
}
-(void)createCommentUserInterface {
//UILabel *lblComments = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 570.0f, 150.0f, 30.0f)];
//UILabel *lblComments = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, newHeight + 70.0f, 150.0f, 30.0f)];
UILabel *lblComments = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, newHeight + 45.0f, 150.0f, 30.0f)];
[lblComments setBackgroundColor:[UIColor clearColor]];
[lblComments setText:@"Comments"];
[self.contentView addSubview:lblComments];
UIButton *btnAddComment = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnAddComment addTarget:self
action:@selector(btnAddCommentPressed)
forControlEvents:UIControlEventTouchDown];
[btnAddComment setBackgroundImage:[UIImage imageNamed:@"btn_addcomment.png"] forState:UIControlStateNormal];
//btnAddComment.frame = CGRectMake(20.0f, 610.0f, 140.0f, 40.0f);
//btnAddComment.frame = CGRectMake(20.0f, newHeight + 110.0f, 140.0f, 40.0f);
btnAddComment.frame = CGRectMake(20.0f, newHeight + 80.0f, 140.0f, 40.0f);
[self.contentView addSubview:btnAddComment];
//commentsTableView = [[UITableView alloc]initWithFrame:CGRectMake(20.0f, 660.0f, 280.0f, 600.0f) style:UITableViewStylePlain];
//commentsTableView = [[UITableView alloc]initWithFrame:CGRectMake(20.0f, newHeight + 160.0f, 280.0f, 600.0f) style:UITableViewStylePlain];
commentsTableView = [[UITableView alloc]initWithFrame:CGRectMake(20.0f, newHeight + 130.0f, 280.0f, 600.0f) style:UITableViewStylePlain];
commentsTableView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.4];
commentsTableView.delegate = self;
commentsTableView.dataSource = self;
[self.contentView addSubview:commentsTableView];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.commentsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIColor *separatorColor = [UIColor colorWithRed:1.0/255.0 green:146.0/255.0 blue:173.0/255.0 alpha:1.0];
[tableView setSeparatorColor:separatorColor];
//[tableView setSeparatorColor:[UIColor darkGrayColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.backgroundColor = [UIColor whiteColor];
userComment = [commentsArray objectAtIndex:[indexPath row]];
UILabel *lblSubmittedBy = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 5.0f, 100.0f, 30.0f)];
[lblSubmittedBy setBackgroundColor:[UIColor clearColor]];
[lblSubmittedBy setText:@"Submitted by"];
[cell addSubview:lblSubmittedBy];
UIButton *btnUserName = [UIButton buttonWithType:UIButtonTypeCustom];
btnUserName.frame = CGRectMake(120.0f, 5.0f, 150.0f, 30.0f);
//[btnUserName setTitle:@"ITF" forState:UIControlStateNormal];
[btnUserName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[btnUserName setBackgroundColor:[UIColor clearColor]];
[btnUserName setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btnUserName addTarget:self action:@selector(showUserProfileForUserComment:) forControlEvents:UIControlEventTouchUpInside];
[btnUserName setUserInteractionEnabled:YES];
[cell addSubview:btnUserName];
UILabel *lblOnDate = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 40.0f, 20.0f, 30.0f)];
[lblOnDate setBackgroundColor:[UIColor clearColor]];
[lblOnDate setText:@"on"];
[cell addSubview:lblOnDate];
UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(35.0f, 40.0f, 200.0f, 30.0f)];
[lblDate setBackgroundColor:[UIColor clearColor]];
[cell addSubview:lblDate];
UITextView *txtVwComments = [[UITextView alloc]initWithFrame:CGRectMake(5.0f, 75.0f, 265.0f, 70.0f)];
//UITextView *txtVwComments = [[UITextView alloc]initWithFrame:CGRectMake(5.0f, 75.0f, 265.0f, 0.0f)];
[txtVwComments setBackgroundColor:[UIColor clearColor]];
//txtVwComments.backgroundColor = cell.backgroundColor;
[txtVwComments setFont:[UIFont fontWithName:@"Helvetica" size:17.0f]];
[txtVwComments setEditable:NO];
[cell addSubview:txtVwComments];
[txtVwComments setText:[userComment Comment]];
CGRect frame = txtVwComments.frame;
frame.size.height = txtVwComments.contentSize.height;
CGFloat height = frame.size.height;
NSLog(@"Value of height => %f",height);
txtVwComments.frame = frame;
CGFloat ht1 = height + 20.0f + 75.0f;
//UIButton *btnUpVote = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 160.0f, 16.0f, 16.0f)];
UIButton *btnUpVote = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, ht1 + 10.0f, 16.0f, 16.0f)];
[btnUpVote setBackgroundColor:[UIColor clearColor]];
[btnUpVote setBackgroundImage:[UIImage imageNamed:@"thumb_up.png"] forState:UIControlStateNormal];
[btnUpVote addTarget:self
action:@selector(btnUpVotePressed:)
forControlEvents:UIControlEventTouchDown];
[btnUpVote setTag:[userComment ID]];
[cell addSubview:btnUpVote];
NSLog(@"Value of ht1 => %f",ht1);
lblUpVoteCount = [[UILabel alloc]initWithFrame:CGRectMake(36.0f, ht1 , 30.0f, 30.0f)];
[lblUpVoteCount setBackgroundColor:[UIColor clearColor]];
[lblUpVoteCount setTag:[userComment ID]];
[cell addSubview:lblUpVoteCount];
//NSLog(@"Value of ht2 => %f",ht2);
//UIButton *btnDownVote = [[UIButton alloc]initWithFrame:CGRectMake(90.0f, 160.0f, 16.0f, 16.0f)];
UIButton *btnDownVote = [[UIButton alloc]initWithFrame:CGRectMake(90.0f, ht1 + 10.0f , 16.0f, 16.0f)];
[btnDownVote setBackgroundColor:[UIColor clearColor]];
[btnDownVote setBackgroundImage:[UIImage imageNamed:@"thumb_down.png"] forState:UIControlStateNormal];
[btnDownVote addTarget:self
action:@selector(btnDownVotePressed:)
forControlEvents:UIControlEventTouchDown];
[btnDownVote setTag:[userComment ID]];
[cell addSubview:btnDownVote];
//lblDownVoteCount = [[UILabel alloc]initWithFrame:CGRectMake(116.0f, 150.0f, 40.0f, 30.0f)];
lblDownVoteCount = [[UILabel alloc]initWithFrame:CGRectMake(116.0f, ht1 , 40.0f, 30.0f)];
[lblDownVoteCount setTag:[userComment ID]];
[lblDownVoteCount setBackgroundColor:[UIColor clearColor]];
[cell addSubview:lblDownVoteCount];
UIButton *btnReply = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//btnReply = [[UIButton alloc]initWithFrame:CGRectMake(180.0f, 150.0f, 60.0f, 40.0f)];
btnReply.frame = CGRectMake(180.0f, ht1, 60.0f, 30.0f);
[btnReply setBackgroundColor:[UIColor clearColor]];
[btnReply setBackgroundImage:[UIImage imageNamed:@"btn_reply.png"] forState:UIControlStateNormal];
[btnReply addTarget:self action:@selector(btnReplyPressed:) forControlEvents:UIControlEventTouchDown];
[btnReply setTag:[userComment ID]];
[cell addSubview:btnReply];
float y = ht1 + 40.0f;
for (int i=0; i < [userComment.replyArray count]; i++) {
if ([userComment.replyArray objectAtIndex:i] != nil) {
UITextView *txtVwReply = [[UITextView alloc]initWithFrame:CGRectMake(5.0f, y , 265.0,0.0f)]; //70.0f)];
//UITextView *txtVwReply = [[UITextView alloc]initWithFrame:CGRectMake(40.0f, y , 220.0,0.0f)];
[txtVwReply setUserInteractionEnabled:NO];
[txtVwReply setBackgroundColor:[UIColor clearColor]];
//[txtVwReply setFont:[UIFont fontWithName:@"Helvetica" size:14.0f]];
[txtVwReply setFont:[UIFont fontWithName:@"Helvetica" size:17.0f]];
[cell addSubview:txtVwReply];
commentReply = [userComment.replyArray objectAtIndex:i];
strReply = [NSString stringWithFormat:@"Submitted by %@ \non %@\n%@",[commentReply ReplyUsername],[commentReply ReplyDateAdded],[commentReply ReplyComment]];
//NSLog(@"strReply => %@",strReply);
[txtVwReply setText:strReply];
CGRect frame = txtVwReply.frame;
frame.size.height = txtVwReply.contentSize.height;
CGFloat height = frame.size.height;
NSLog(@"Value of txtVwReply height => %f",height);
txtVwReply.frame = frame;
y = y + height;
//y = y + 80;
[txtVwReply release];
}
}
[btnUserName setTitle:[userComment Username] forState:UIControlStateNormal];
[lblDate setText:[userComment DateAdded]];
[lblUpVoteCount setText:[NSString stringWithFormat:@"%i",[userComment UpVotes]]];
[lblDownVoteCount setText:[NSString stringWithFormat:@"%i",[userComment DownVotes]]];
[lblSubmittedBy release];
//[btnUserName release];
[lblOnDate release];
[lblDate release];
[txtVwComments release];
[btnUpVote release];
[lblUpVoteCount release];
[btnDownVote release];
[lblDownVoteCount release];
return cell;
}
#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([commentsArray count]>0) {
userComment = [commentsArray objectAtIndex:indexPath.row];
NSString *strComment =userComment.Comment;
//NSLog(@"strComment : - %@",strComment);
NSString *strCommentReply;
CGFloat replyHeight = 135.0f;
if([userComment.replyArray count]>0) {
for (int i=0; i < [userComment.replyArray count]; i++) {
commentReply = [userComment.replyArray objectAtIndex:i];
//NSString *
strCommentReply = commentReply.ReplyComment;
//NSLog(@"strCommentReply => %@",strCommentReply);
// Append all the reply & then use to determine hight
[strCommentReply stringByAppendingFormat:@"\n %@",strCommentReply];
replyHeight = replyHeight + 120.0f;
}
}
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize sizeComment = [strComment sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(sizeComment.height + replyHeight + 40.0f, 44.0f);
NSLog(@"heightForRowAtIndexPath >> height %f",height);
return height;
}
NSLog(@"heightForRowAtIndexPath >> TABLE_CELL_HEIGHT - %f",TABLE_CELL_HEIGHT);
return TABLE_CELL_HEIGHT;
}
所以我需要計算相應的設置表視圖&內容視圖的框架表視圖&的框架。我怎樣才能做到這一點?
這是怎麼應用的樣子現在
歐文,你說得對。我添加內容視圖使用[self.scrollView addSubview:self.contentView]將視圖作爲子視圖滾動;我使用內容視圖來顯示標籤,按鈕和文本視圖,而不是直接放在滾動視圖上。 – iOSAppDev
但它不能解決問題。我只是添加內容視圖作爲子視圖滾動視圖使用編程 – iOSAppDev
您可以使用CGRect臨時變量來設置您的框架:CGRect myFrame = tableview.frame; myFrame.origin.y = 100; myFrame.origin.x = 100; [tableview setFrame:myFrame]; –