【发布时间】:2015-02-18 13:32:18
【问题描述】:
我使用 parse 来存储用户 cmets,并在 UITableViewController 中显示这些。我正在尝试使 UITableViewCell 动态化,以便单元格随 UILabel 的大小而变化。我得到了奇怪的结果。
我的commentTableViewController.m
#import "CommentsTableViewController.h"
#import <Parse/Parse.h>
#import "AddCommentViewController.h"
#import "CustomTableViewCell.h"
@interface CommentsTableViewController ()
@property (strong, nonatomic) CustomTableViewCell *customCell;
@end
@implementation CommentsTableViewController
-(void)viewDidLoad
{
[self getWallImages];
}
-(void)viewDidAppear:(BOOL)animated
{
[self getWallImages];
[self.tableView reloadData];
}
-(void) viewWillAppear: (BOOL) animated
{
[self getWallImages];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.commentsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
PFObject *commentObject = [self.commentsArray objectAtIndex:indexPath.row];
cell.userLabel.text = [commentObject objectForKey:@"user"];
cell.commentLabel.text = [commentObject objectForKey:@"comment”];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.titleNameString;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(!self.customCell) {
self.customCell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
}
PFObject *commentObject = [self.commentsArray objectAtIndex:indexPath.row];
self.customCell.userLabel.text = [commentObject objectForKey:@"user"];
self.customCell.commentLabel.text = [commentObject objectForKey:@"comment"];
self.customCell.commentLabel.preferredMaxLayoutWidth = self.tableView.bounds.size.width;
[self.customCell layoutIfNeeded];
CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
-(void)getWallImages
{
PFQuery *query = [PFQuery queryWithClassName:[NSString stringWithFormat:@"%@",self.titleNameString]];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.commentsArray = nil;
self.commentsArray = [[NSArray alloc] initWithArray:objects];
[self.tableView reloadData];
} else {
NSString *errorString = [[error userInfo] objectForKey:@"error"];
UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[errorAlertView show];
}
}];
}
不确定这是否与故事板中我的 UITableViewCell 的设置或我的 UILabel 约束有关......
如果我将 commentLabel 的优先级约束设置为小于 500(例如 250),我会得到这个(标签覆盖所有单元格并且通常不起作用)
如果优先级大于 500,则单元格中不会显示任何内容。
【问题讨论】:
-
能否也显示自动布局代码?
-
我在哪里可以找到它?我只在故事板中使用过它
-
也许你可以在你的故事板中制作一个约束的截图?
-
添加了约束的屏幕截图
标签: ios objective-c iphone xcode uitableview