我制作了一个iPad应用程序,因为我有一个名为crollTableView的tableView,在我的tableView中,我以肖像模式在行的每个单元格中创建了两个标签,它们的名称分别为capitalLabel和stateLabel,

现在我想在横向模式下增加标签的宽度,

这是代码段,

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if(tableView==crollTableView){
        static NSString *CellIdentifier=@"Cell";
        static NSInteger StateTag = 1;
        static NSInteger CapitalTag = 2;
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil){

            // cell.backgroundColor=[UIColor yellowColor];
            cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
            CGRect frame;
            frame.origin.x = 0;
            frame.origin.y = 0;
            frame.size.height = 70;
            frame.size.width = 650;


            UILabel *capitalLabel = [[UILabel alloc] initWithFrame:frame];
            capitalLabel.tag = CapitalTag;
            capitalLabel.opaque = FALSE;
            capitalLabel.font=[UIFont systemFontOfSize:20.0];
            [cell.contentView addSubview:capitalLabel];


            frame.origin.x += 680;
            UILabel *stateLabel = [[UILabel alloc] initWithFrame:frame];
            stateLabel.tag = StateTag;
            stateLabel.font=[UIFont systemFontOfSize:17.0];
            [cell.contentView addSubview:stateLabel];
        }

        UILabel* capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag];
        UILabel*  stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag];

        capitalLabel.text=[news2 objectAtIndex:indexPath.row];
        stateLabel.text = [news3 objectAtIndex:indexPath.row];

       capitalLabel.backgroundColor = [UIColor clearColor];
        stateLabel.backgroundColor = [UIColor clearColor];

        capitalLabel.textColor=[UIColor whiteColor];
        stateLabel.textColor=[UIColor whiteColor];

        if(count<[news3 count]){
             capitalLabel.numberOfLines = 0;
            [capitalLabel sizeToFit];

            stateLabel.numberOfLines = 0;
            [stateLabel sizeToFit];

        }count++;

        return cell;
    }
 }


我应该对代码进行哪些更改?

最佳答案

您应该覆盖

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }


通过检查当前方向是横向还是纵向的条件,可以相应地设置标签框架。

关于iphone - 在横向模式下无法增加UILabel的宽度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8876260/

10-14 22:00
查看更多