我已经通过autoresizingMask
设置了customcell UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;
属性的内容视图,它工作正常但是当我旋转设备内容的屏幕时,视图宽度改变了很好但是当我滚动表格时,contenview的宽度发生了变化(它在委托方法tableView中计算大小:tableView cellForRowAtIndexPath: indexPath
)。上面的问题和第二个问题是在滚动之后然后从横向旋转回到portait形式,内容视图的宽度变小。
preference* pref = [[preference alloc] init];
[pref initialize];
userInfo *mahesh=[pref getUserInfo];
NSLog(@"index of cell****** %d",indexPath.section);
static NSString *CellIdentifier = @"CustomCell2";
imTextVO *temp=[masterArray objectAtIndex:(indexPath.section)];
CustomCell2 *cell1 =(CustomCell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *cellValue = [commonArray objectAtIndex:(indexPath.section)];
if (cell1==nil)
{
cell1 = [[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// UIImageView *image=[[UIImageView alloc] init];
//
// [cell1.contentView addSubview:image];
// if (temp.type==2)
// {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// button.frame=CGRectMake(110, 45, 100,40);
[button addTarget:self action:@selector(viewAttachmentOfEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"View Attachment" forState:UIControlStateNormal];
button.titleLabel.font=[UIFont systemFontOfSize:12.0f];
button.titleLabel.textColor=[UIColor blackColor];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
// [button setTag:(indexPath.section)];
button.tag=22;
[cell1.contentView addSubview:button];
}
cell1.backgroundColor=[UIColor clearColor];
CGSize constraint1 = CGSizeMake(160+100-140, 20000.0f);
CGSize size1 = [[ArrayForActor objectAtIndex:indexPath.section] sizeWithFont:[UIFont systemFontOfSize:16.0f] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap];
CGSize constraint = CGSizeMake(160+100, 20000.0f);
CGSize size = [cellValue sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (temp.type==2) {
if ([temp.mobileNo isEqualToString:mahesh.mobileno])
{
if (size.width>(size1.width+180)) {
cell1.ViewContainer.frame=CGRectMake(0,0,size.width+4 ,size1.height+50);
}else{
cell1.ViewContainer.frame=CGRectMake(0,0,size1.width+4+180 ,size1.height+50);
}
cell1.ViewContainer.backgroundColor=[UIColor yellowColor];
cell1.dateText.frame=CGRectMake(cell1.ViewContainer.frame.size.width-140,cell1.ViewContainer.frame.origin.y ,135 ,20);
}else{
if (size.width>(size1.width+180)) {
cell1.ViewContainer.frame=CGRectMake(self.view.frame.size.width-size.width+4,0,size.width+4 ,size1.height+50);
}else{
cell1.ViewContainer.frame=CGRectMake(self.view.frame.size.width-size1.width-180+4,0,size1.width+4+180 ,size1.height+50);
}
cell1.dateText.frame=CGRectMake(self.chatTable.frame.size.width-140,cell1.ViewContainer.frame.origin.y ,135 ,20);
cell1.ViewContainer.backgroundColor=[UIColor greenColor];
}
}else{
if ([temp.mobileNo isEqualToString:mahesh.mobileno])
{
if (size.width>(size1.width+180)) {
cell1.ViewContainer.frame=CGRectMake(0,0,size.width+4 ,size.height+4+size1.height+4);
}else{
cell1.ViewContainer.frame=CGRectMake(0,0,size1.width+4+180 ,size.height+4+size1.height+4);
}
cell1.ViewContainer.backgroundColor=[UIColor yellowColor];
cell1.dateText.frame=CGRectMake(cell1.ViewContainer.frame.size.width-140,cell1.ViewContainer.frame.origin.y ,135 ,20);
}else{
if (size.width>(size1.width+180)) {
cell1.ViewContainer.frame=CGRectMake(self.view.frame.size.width-size.width-4,0,size.width+4 ,size.height+4+size1.height+4);
}else{
cell1.ViewContainer.frame=CGRectMake(self.view.frame.size.width-size1.width-180-4,0,size1.width+4+180 ,size.height+4+size1.height+4);
}
cell1.dateText.frame=CGRectMake(self.chatTable.frame.size.width-140,cell1.ViewContainer.frame.origin.y ,135 ,20);
cell1.ViewContainer.backgroundColor=[UIColor greenColor];
}
}
cell1.actorLabel.frame=CGRectMake(cell1.ViewContainer.frame.origin.x+2,cell1.ViewContainer.frame.origin.y,size1.width+4,size1.height+4);
cell1.actorLabel.text=[ArrayForActor objectAtIndex:indexPath.section];
cell1.chatText.frame=CGRectMake(cell1.ViewContainer.frame.origin.x,cell1.actorLabel.frame.size.height-12, cell1.ViewContainer.frame.size.width,size.height+4+4);
UIButton *thisInviteButton = (UIButton*)[cell1.contentView viewWithTag:22];
thisInviteButton.hidden=YES;
if (temp.type==2) {
thisInviteButton.hidden=NO;
thisInviteButton.frame=CGRectMake((cell1.ViewContainer.frame.size.width/2)-50,size1.height, 100,40);
}else{
}
cell1.dateText.textAlignment=UITextAlignmentRight;
cell1.chatText.textAlignment = UITextAlignmentLeft;
cell1.chatText.text= cellValue;
cell1.selectionStyle=NO;
NSTimeInterval interval=[temp.createdDate longLongValue];
NSDate *currentTime=[NSDate dateWithTimeIntervalSince1970:interval/1000];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSNumber *myDateInString=[NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970]*1000];
NSTimeInterval inte=[myDateInString longLongValue];
NSDate *todayTime=[NSDate dateWithTimeIntervalSince1970:inte/1000];
NSString * todayString = [[todayTime description] substringToIndex:10];
NSString *messageDate=[[currentTime description] substringToIndex:10];
if ([todayString isEqualToString:messageDate]) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"hh.mm" options:0
locale:gbLocale];
[dateFormatter setDateFormat:formatString];
[dateFormatter setLocale:gbLocale];
cell1.dateText.text=[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:currentTime]];
}else {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"dMMMyy,hh.mm" options:0
locale:gbLocale];
[dateFormatter setDateFormat:formatString];
[dateFormatter setLocale:gbLocale];
cell1.dateText.text=[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:currentTime]];
}
return cell1;
答案 0 :(得分:0)
我不会根据您的代码直接回答,我只是给您一些建议:
尽可能保持-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
的清洁。如果您要进行繁重的计算,请在实际[table reloadData]
之前进行。否则你会给你的用户一个糟糕的体验,因为每次他滚动UITableView
时,都会计算出重量级的东西。
如果获取单元格最终大小的计算很重(具有多个子项的复杂UITableViewCell
),请在重新加载表之前执行这些计算。这里的重点是为用户提供良好的体验。
保持代码注释,如果你在几个月后拿到这个项目,你会感谢你自己。
通常,你可以通过XIB上的一些自动调整魔法来达到你想要的大小。尽管如此,您可以根据self.interfaceOrientation
确定大小的计算(参见第2点),然后您可以将这些值(纵向和横向)存储在字典中,然后由- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
使用
不要直接访问UITableViewCell's
属性。而是创建一个名为(例如..)- (void)updateWithDictionary:(NSDictionary *)cellInformation
的方法。这样您就可以解耦代码,从而创建更独立,更清晰的组件。在这个cellInformation
中,您可以传递您的单元格所需的所有数据。