UITableCell宽度滚动时调整大小

时间:2013-09-14 21:28:24

标签: ios6 uitableview

我遇到了UITableViewController的问题,只要向下滚动,单元格的背景图像就会增大。所有图像都设置为320px的宽度,但在滚动单元格背景图像后仍然会展开。

enter image description here

任何人都可以解释为什么会发生这种情况以及如何解决这个问题吗?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (jsonResults.count == 0)
    {
        return 1;
    }
    else
    {
    return [jsonResults count];
    }
}

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

    static NSString *CellIdentifier = @"Cell";
    UIImage *selectionBackground;
    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];

    if (jsonResults.count == 0)
    {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"topAndBottomRow.png"]
                                                                  stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
        selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
        [cell.selectedBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:selectionBackground]];
        cell.textLabel.text = @"Nothing was found :(";
        cell.detailTextLabel.text = @"Click here to add a venue!";
        //cell.textLabel.textColor= [UIColor colorWithRed:(79/255.0) green:(129/255.0) blue:(189/255.0) alpha:1] ;
        cell.textLabel.font = [UIFont fontWithName:@"Berlin Sans FB" size:22];
        cell.detailTextLabel.font = [UIFont fontWithName:@"Berlin Sans FB" size:14];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    else
    {
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    cell.userInteractionEnabled = YES;
    NSDictionary *venuesdict = [jsonResults objectAtIndex:indexPath.row];
    NSString *addresslineone = [venuesdict objectForKey:@"address1"];
    NSString *postcode = [venuesdict objectForKey:@"postcode"];
    NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
    NSInteger row = [indexPath row];
   if (row == 0 && row == sectionRows - 1)
   {
    //rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"topAndBottomRow.png"]
                                                                 stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
    selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];

    }
    else if (row == 0)
    {
        //rowBackground = [UIImage imageNamed:@"topRow.png"];
        cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"topRow.png"]
                                                                  stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];


    }
    else if (row == sectionRows - 1)
    {
        //rowBackground = [UIImage imageNamed:@"bottomRow.png"];
        cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"bottomRow.png"]
                                                                  stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        //rowBackground = [UIImage imageNamed:@"bottomRow.png"];

        selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];


    }
    else
    {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"middleRow.png"]
                                                                  stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        //rowBackground = [UIImage imageNamed:@"middleRow.png"];
        selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];


    }
    UIImageView *imgView =  [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.frame];
    [imgView setImage:selectionBackground];
    [cell.selectedBackgroundView addSubview:imgView];
    [cell.selectedBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:selectionBackground]];
    cell.textLabel.text = [venuesdict objectForKey:@"name"];
    NSString *imagename = [venuesdict objectForKey:@"imagename"];
    cell.imageView.image = [UIImage imageNamed:imagename];
    cell.detailTextLabel.text = [NSString stringWithFormat:@" %@, %@",addresslineone,postcode];
    cell.textLabel.font = [UIFont fontWithName:@"Berlin Sans FB" size:22];
    cell.detailTextLabel.font = [UIFont fontWithName:@"Berlin Sans FB" size:10];
    cell.backgroundColor = [UIColor clearColor];
        NSString *version = [[UIDevice currentDevice] systemVersion];
        BOOL isAtLeast7 = [version floatValue] >= 7.0;
        if (!isAtLeast7 == YES)
        {
            cell.detailTextLabel.backgroundColor = [UIColor grayColor];
            cell.textLabel.backgroundColor = [UIColor grayColor];
        }

    }

    return cell;
    }

1 个答案:

答案 0 :(得分:0)

尝试更改此行

    UIImageView *imgView =  [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.frame];

进入这个

    UIImageView *imgView =  [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.bounds];