0

我已经设置了 customcell autoresizingMask属性的内容视图,UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;它工作正常,但是当我旋转设备内容视图的屏幕时,宽度改变了,但是当我滚动表格时,contenview 的宽度发生了变化(它在委托方法 tableView 中计算大小:)tableView cellForRowAtIndexPath: indexPath。上述一个问题和第二个问题是在滚动之后从横向旋转回纵向形式,内容视图的宽度变小。

    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;
4

1 回答 1

0

我不会根据你的代码直接回答,我只是给你一些建议:

  1. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath尽可能保持清洁。如果您有繁重的计算要做,请在实际操作之前进行[table reloadData]。否则你会给你的用户一个糟糕的体验,因为他每次滚动UITableView,都会计算一些很重的东西。

  2. 如果获取单元格最终大小的计算量很大(复杂UITableViewCell的有多个子项),请在重新加载表格之前执行这些计算。这里的重点是给用户一个良好的体验。

  3. 保持你的代码注释,你会感谢自己,如果你几个月后拿起这个项目。

  4. 通常,您可以通过 XIB 上的一些自动调整大小的魔法来实现您想要的大小。尽管如此,您仍然可以self.interfaceOrientation基于- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.

  5. 不要UITableViewCell's直接访问属性。而是创建一个名为 (for example..) 的方法- (void)updateWithDictionary:(NSDictionary *)cellInformation。通过这种方式,您可以解耦代码,创建更独立、更简洁的组件。在这种情况cellInformation下,您可以传递您的单元所需的所有数据。

于 2013-03-08T13:40:03.970 回答