0

I want to display Image,Title name,and two left right buttons. That I have done successfully. Here is the code:

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];

    UIButton *leftButton = [[UIButton alloc]init];
    [leftButton setImage:[UIImage imageNamed:@"previous-arrow.png"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(goPrevious:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setFrame:CGRectMake(20, 50, 14, 13)];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Title.png"]];
    [imageView setFrame:CGRectMake(leftButton.frame.origin.x+leftButton.frame.size.width+4, 20, 50, 50)];

    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+4, 30, 100, 40)];
    NSString *cName = [NSString stringWithFormat:@"%@",[NameArray objectAtIndex:indexPath.section]];

    [nameLabel setTextColor:[UIColor whiteColor]];
    [nameLabel setBackgroundColor:[UIColor clearColor]];
    [nameLabel setContentMode:UIViewContentModeScaleAspectFill];
    [nameLabel setTextAlignment:UITextAlignmentLeft];
     CGSize expectedLabelSize = [cName sizeWithFont:nameLabel.font 
                                        constrainedToSize:CGSizeMake(100, 40) 
                                            lineBreakMode:nameLabel.lineBreakMode]; 
    [nameLabel setFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+4, 35, expectedLabelSize.width, expectedLabelSize.height)];
    nameLabel.text=cName;

    UIButton *rightButton = [[UIButton alloc]init];
    [rightButton setImage:[UIImage imageNamed:@"next-arrow.png"] forState:UIControlStateNormal];
    [rightButton setFrame:CGRectMake(nameLabel.frame.origin.x+nameLabel.frame.size.width+4, 50, 14, 13)];
    [rightButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:nameLabel];
    [view addSubview:imageView];
    [view addSubview:leftButton];
    [view addSubview:rightButton];

    self.navigationItem.titleView = view; 

This code is written in cellForRowAtIndexPath. The view is showing properly on navigation bar but after scrolling the tableview. Initially it is displaying on wrong coordinates.

Please Suggest.

4

1 に答える 1

2

Put this code in viewWillAppear or viewDidAppear. It's not meant to be in tableView protocol!

于 2012-08-09T06:56:39.253 に答える