动态管理表高度 原则就是先计算显示内容的高度
- (float)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
if(tblviewSign==tableView){//签名
CGSizesize=[personInfor.codeContentsizeWithFont:[UIFontsystemFontOfSize:12]constrainedToSize:CGSizeMake(260,88)lineBreakMode:NSLineBreakByWordWrapping];
[tblviewSignframeSetHeight:44+size.height+20];//设置表的高度
return44+size.height;
}
return42;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self tableView:tableView heightForRow:indexPath];
}
//此方法是自定义方法,方法内容是cellForRow中的方法copy过来,然后稍作修改即可(就算以后cell内容也修改,此方法也不用大改),然后在heightForRowAtIndexPath协议方法中调用
- (CGFloat)tableView:(UITableView *)tableView heightForRow:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
SUStoreChatlogs *chatlog = [arrayMsg objectAtIndex:indexPath.row];
float returnCellHeight = 0.0;
cell = [self makeCustomCell:tableView identifier:@"receivedIdentifier" loadNibNamed:@"MsgCellReceived"];
SUVButtonReceived *bubbleReceived = (SUVButtonReceived *)[cell viewWithTag:4];
UIImageView *imgviewAvatar = (UIImageView *)[cell viewWithTag:1];
UILabel *lblTime = (UILabel *)[cell viewWithTag:3];
[self viewReceived:chatlog bubble:bubbleReceived lbl:lblTime];
returnCellHeight = MAX(bubbleReceived.frame.size.height + bubbleReceived.frame.origin.y, lblTime.frame.size.height + imgviewAvatar.frame.size.height);
return returnCellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
SUSeekuDbHelper *db = [[SUSeekuDbHelper alloc]init];
SUStoreChatlogs *chatlog = [arrayMsg objectAtIndex:indexPath.row];
cell = [self makeCustomCell:tableView identifier:@"receivedIdentifier" loadNibNamed:@"MsgCellReceived"];
SUVButtonReceived *bubbleReceived = (SUVButtonReceived *)[cell viewWithTag:4];
UIImageView *imgviewAvatar = (UIImageView *)[cell viewWithTag:1];
UILabel *lblTime = (UILabel *)[cell viewWithTag:3];
//头像
[imgviewAvatar setImageWithURL:[NSURL URLWithString:personMsg.avatar] placeholderImage:[SUPackMethod dgGetDefaultAvatarFromSex:(SU_SEX)personMsg.sex]];
[self viewReceived:chatlog bubble:bubbleReceived lbl:lblTime];
CGRect cellFrame = cell.frame;
cellFrame.size.height = bubbleReceived.frame.size.height + bubbleReceived.frame.origin.y;
cell.frame = cellFrame;
[db release];
return cell;
}
刷新表时定位问题
if ([arrayMsgcount] > 0) {
//这个条件必须要做判断,因为如果arrayMsgcount为0的话,[arrayMsgcount]-1为负,会导致崩溃
[tblviewSMSreloadData];
[tblviewSMSscrollToRowAtIndexPath:[NSIndexPathindexPathForRow:[arrayMsgcount]-1 inSection:0]atScrollPosition:UITableViewScrollPositionBottomanimated:NO];
}
表单元格样式 (单元格样式的内容,可以在layoutSubviews中定义)
@interface SUInviteByPhoneCell : UITableViewCell
@end
@implementation SUInviteByPhoneCell
- (void) layoutSubviews {
[super layoutSubviews];
self.textLabel.font = [UIFont systemFontOfSize:15];
self.detailTextLabel.font = [UIFont systemFontOfSize:15];
self.detailTextLabel.textColor = [UIColor colorToGRB:@"9e9e9e"];
self.detailTextLabel.backgroundColor = [UIColor clearColor];
self.detailTextLabel.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
self.detailTextLabel.textAlignment = UITextAlignmentCenter;
//这里定义了accessoryView的坐标大小
self.accessoryView.frame = CGRectMake(265, 6, 25, 25);
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
SUInviteByPhoneCell *cell = (SUInviteByPhoneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[SUInviteByPhoneCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}