どうすればこれを達成できますか? 問題に関する有用な情報が見つかりません....アプリにフォーラムがあり、投稿の大きさに応じて、セルはテキストの量に合わせて拡大縮小する必要があります...これを設定することは可能ですか?ストーリーボードですか、それともプログラムすることを余儀なくされていますか? /よろしく
EDIT以下は、tableView の私のコードです。セルには 3 つのラベルがあり、2 つは多かれ少なかれ静的 (常に同じ量のテキスト) で、3 つ目は動的 (commentLabel) で、たとえば 1 行から 70 行までのすべてを含むことができます。今、commentLabelの高さの後にセルを拡張したい...
#import "ForumthreadViewController.h"
@interface ForumthreadViewController ()
@end
@implementation ForumthreadViewController
@synthesize tableView;
@synthesize textField;
@synthesize refreshButton;
@synthesize forumThreadDataProvider;
@synthesize dateFormatter;
@synthesize items;
@synthesize taskId;
@synthesize forumThreadId;
- (void)viewDidLoad
{
[super viewDidLoad];
self.forumThreadDataProvider = [[MLForumThreadDataProvider alloc] initWithDelegate:self];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:self.refreshButton, [self initLoadingIndicator], nil];
NSLocale *locale = [NSLocale currentLocale];
self.dateFormatter = [[NSDateFormatter alloc] init];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"E MMM d" options:0 locale:locale];
[self.dateFormatter setDateFormat:dateFormat];
[self.dateFormatter setLocale:locale];
[self refreshData];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)hidesBottomBarWhenPushed
{
return TRUE;
}
- (void)setLoadingState : (BOOL)loading
{
[super setLoadingState:loading];
if (loading)
{
[self.refreshButton setEnabled:NO];
}
else
{
[self.refreshButton setEnabled:YES];
}
}
#pragma mark - Text field
- (void)textFieldDidBeginEditing:(UITextField *)pTextField
{
[self animateTextField: pTextField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)pTextField
{
[self animateTextField: pTextField up: NO];
[pTextField resignFirstResponder];
}
- (void) animateTextField: (UITextField*) pTextField up: (BOOL) up
{
const int movementDistance = 215; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)pTextField
{
[self setLoadingState:YES];
[pTextField resignFirstResponder];
NSUserDefaults *userStorage = [NSUserDefaults standardUserDefaults];
NSString *alias = [self urlEncode:[userStorage objectForKey:@"alias"]];
NSString *email = [self urlEncode:[userStorage objectForKey:@"email"]];
NSString *who = [self getUniqueDeviceId];
NSString *comment = [self urlEncode:[pTextField text]];
comment = [comment stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
who = [who stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([self isBlank:comment])
{
[self setLoadingState:NO];
pTextField.text = @"";
return NO;
}
if([self isBlank:alias])
{
[self showMessagePopup:NSLocalizedString(@"MessageMustChooseAlias", nil)];
return NO;
}
[self.forumThreadDataProvider startSendPost:self.taskId : self.forumThreadId : alias : who : email : comment];
pTextField.text = @"";
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count];
}
- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ForumthreadCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Feedback *item = [self.items objectAtIndex:indexPath.row];
UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];
[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];
commentLabel.numberOfLines = 0;
// commentLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
//commentLabel.lineBreakMode = UILineBreakModeWordWrap;
[commentLabel sizeToFit];
// CGSize constraint = CGSizeMake(300, 2000);
// CGSize size = [item.comment sizeWithFont:[UIFont boldSystemFontOfSize:11] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
//[commentLabel setFrame:CGRectMake(16, 30, 300, size.height)];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell) {
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
return commentLabel.frame.size.height;
}
else
return 30;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#pragma mark - Fetch data
- (IBAction) refreshData
{
[self setLoadingState:YES];
[self.forumThreadDataProvider startFetchResult:self.taskId : self.forumThreadId];
//[self fetchForumPosts:self.taskId : self.feedbackId];
}
- (void) onFetchResultDone : (NSArray *) result
{
self.items = result;
[self.tableView reloadData];
[self setLoadingState:NO];
}
- (void) onCreatePostDone : (Result *) result
{
if ([result success])
{
[self refreshData];
}
else
{
[self showMessagePopup:[NSString stringWithFormat:NSLocalizedString(@"MessageErrorCreateForumPost", nil), result.code]];
}
[self setLoadingState:NO];
}
- (void) onForumRequestFail : (NSError *) result
{
[self showNoConnectionPopup];
[self setLoadingState:NO];
}