我目前使用PFQueryTableViewController,我的UITableView
看起来像标准TableView
我希望它在视图中看起来像每个项目的一个圆角矩形。根据以下答案,我的代码目前是:
Cell.m
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(1,1,69,69);
}
+ (CGFloat)heightForCellWithName:(NSString *)name contentString:(NSString *)content
{
CGSize contentSize = [content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20.0, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin // wordwrap?
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.0f]}
context:nil].size;
return 10 + 50 + 10 + contentSize.height + 10 + 20 + 10 + 30;
}
PFQueryTableViewController
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
PFObject *entry = [self.objects objectAtIndex:indexPath.row];
NSString *commentString = entry[@"Request"];
NSString *nameString = @"";
NSLog(@"%@", commentString);
return [Cell heightForCellWithName:nameString contentString:commentString] + 50;
}
这样做会返回:
更新
虽然主要问题已经解决,但我注意到如果contentLabel.text太大,它会开始掩盖likeButton和commentButtons。
答案 0 :(得分:1)
你必须根据文本的内容动态设置单元格的高度...我已经用我的项目做了这个并且非常简单...创建一个具有类方法的自定义UITableviewcell +(CGFloat)heightForCellWith:(NSString *)content
并在返回当前可见单元格的高度时返回调用它......下面是一些示例代码
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *commentString = [self.objects[indexPath.row] objectForKey:kPHGlobalChatTextKey];
PFUser *commentAuthor = (PFUser *)[object objectForKey:kPHGlobalChatUserKey];
NSString *nameString = @"";
if (commentAuthor) {
nameString = [commentAuthor objectForKey:kPHUserDisplayNameKey];
}
return [PHChatCell heightForCellWithName:nameString contentString:commentString] + 50;
}
基本上我是根据传入的文本设置单元格的高度...下面是该方法的实现方式:
+ (CGFloat)heightForCellWithName:(NSString *)name contentString:(NSString *)content
{
CGSize contentSize = [content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20.0, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin // wordwrap?
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.0f]}
context:nil].size;
/*
* header spacing
* profile picture
* spacing
* content
* spacing
* like/comment label
* spacing
* like/comment button
*/
return 10 + 50 + 10 + contentSize.height + 10 + 20 + 10 + 30;
}
正如您所看到的,我甚至不使用用户名来确定单元格的高度,只是传递给方法的字符串的内容......
看到结果的照片......
是的......在创建标签时......您需要设置[label numberOfLines:0]
。
<强> 修改 强>
查看我用于屏幕截图的单元格...它应该为您清理一下并让您更好地了解最新情况......如何布置标签以及如何设置所有内容... < / p>
有一种方法未包含在调用PHUtility
的Cell类中。只是评论一下你的好处......它基本上处理日期应该如何格式化当前日期。
<强> 修改 强>
如果设置了内容标签高度,请将值更改为此...
CGSize contentSize = [content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40.0, FLT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.0f]}
context:nil].size;
注意-40
...你需要在两个地方更改它,layoutsubviews和heightForCell类方法
将NSFontAttributeName设置为您喜欢的任何内容,只需确保在heightForCell类方法和layoutsubviews中返回相同的CGSize变量。
由于单元格视图的x值原点为10且内容标签位于单元格视图中,其中内容标签x原点值也为10,因此在主屏幕内实际x值为20,因此宽度需要为主屏幕的宽度-40。
另外,继续使用我用来格式化日期的方法......
#pragma mark - Date formatters
+(NSDateFormatter *)timeFormatter
{
static dispatch_once_t onceMark;
static NSDateFormatter *formatter = nil;
dispatch_once(&onceMark, ^{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"h:mm a"];
});
return formatter;
}
+(NSDateFormatter *)formatter
{
static dispatch_once_t onceMark;
static NSDateFormatter *formatter = nil;
dispatch_once(&onceMark, ^{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM d 'at' h:mm a"];
});
return formatter;
}
+ (NSString *)relativeDateStringForDate:(NSDate *)date
{
#ifdef __IPHONE_8_0
NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitWeekOfYear | NSCalendarUnitMonth | NSCalendarUnitYear;
#else
NSCalendarUnit units = NSDayCalendarUnit | NSWeekOfYearCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
#endif
// if `date` is before "now" (i.e. in the past) then the components will be positive
NSDateComponents *components = [[NSCalendar currentCalendar] components:units
fromDate:date
toDate:[NSDate date]
options:0];
if (components.year > 0) {
return [[PHUtility formatter] stringFromDate:date];
} else if (components.month > 0) {
return [[PHUtility formatter] stringFromDate:date];
} else if (components.weekOfYear > 0) {
return [[PHUtility formatter] stringFromDate:date];
} else if (components.day > 0) {
return (components.day == 1) ?
[[PHUtility formatter] stringFromDate:date] :
[NSString stringWithFormat:@"Yesterday at %@", [[PHUtility timeFormatter] stringFromDate:date]];
}
if (components.year == 0 && components.month == 0 && components.weekOfYear == 0 && components.day == 0){
return [NSString stringWithFormat:@"Today at %@", [[PHUtility timeFormatter] stringFromDate:date]];
}
return nil;
}
<强> 修改 强>
为了帮助您解决按钮选择问题,每次用户设置祷告时,您都必须重新设置UIButtons背景颜色...
以下是我为了您的问题而实施该方法的方法:
- (void)setLikeStatus:(BOOL)liked {
[self.likeButton setSelected:liked];
if (liked) {
[self.likeImage setImage:[UIImage imageNamed:@"ButtonLikeSelected.png"]];
[self.likeButton setBackgroundColor:[UIColor blueColor]];
return;
}
[self.likeImage setImage:[UIImage imageNamed:@"ButtonLike.png"]];
[self.likeButton setBackgroundColor:buttonColor];
}