2012-01-15 25 views
3

我的UITableViewCell存在問題。我正在開發一個應用程序,其中某個Feed中的某些帖子可能包含一個圖片,並且點擊的按鈕還需要包含一個「標籤」號碼,具體取決於表格的哪一行,因爲我已下載一組圖片。在UITableViewCell中使用UIImage的UIButton - 重疊等

問題在於當我滾動(如你已經猜到的)表格時。圖像加載正常,但是當我向上/向下滾動時,圖像將會在其他單元格上方移動,並且當單元格顯示回屏幕上時,圖像將快速將其位置改變到正確的位置等。

這裏是我的UITableView的代碼:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [feed count]; 
} 

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier 
{ 
    CGRect rect = [self.tableView bounds]; 
    CGRect CellFrame = CGRectMake(0.0f, 0.0f, rect.size.width, 10.0f); 
    CGRect TitleFrame = CGRectMake(55.0f, 10.0f, 240.0f, 15.0f); 
    CGRect TextFrame = CGRectMake(55.0f, 25.0f, 250.0f, 15.0f); 
    CGRect PinFrame = CGRectMake(21.0f, 10.0f, 30.0f, 30.0f); 
    CGRect ViewFrame = CGRectMake(50.0f, 35.0f, 260.0f, 0.0f); 
    CGRect CommentsFrame = CGRectMake(300.0f, 10.0f, 20.0f, 15.0f); 
    UILabel * lblTemp; 

    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    [cell setFrame:CellFrame]; 

    lblTemp = [[UILabel alloc] initWithFrame:TitleFrame]; 
    lblTemp.tag = 1; 
    lblTemp.numberOfLines = 0; 
    lblTemp.opaque = NO; 
    lblTemp.font = [UIFont systemFontOfSize:13.0f]; 
    lblTemp.backgroundColor = [UIColor clearColor]; 
    lblTemp.textColor = [UIColor blackColor]; 
    lblTemp.textAlignment = UITextAlignmentLeft; 
    lblTemp.shadowColor = [UIColor whiteColor]; 
    lblTemp.shadowOffset = CGSizeMake(0, 1); 
    [cell.contentView addSubview:lblTemp]; 

    lblTemp = [[UILabel alloc] initWithFrame:TextFrame]; 
    lblTemp.tag = 2; 
    lblTemp.numberOfLines = 0; 
    lblTemp.lineBreakMode = UILineBreakModeWordWrap; 
    lblTemp.adjustsFontSizeToFitWidth = NO; 
    lblTemp.font = [UIFont systemFontOfSize:13.0f]; 
    lblTemp.textColor = [UIColor lightGrayColor]; 
    lblTemp.backgroundColor = [UIColor clearColor]; 
    lblTemp.shadowColor = [UIColor whiteColor]; 
    lblTemp.shadowOffset = CGSizeMake(0, 1); 
    [cell.contentView addSubview:lblTemp]; 

    lblTemp = [[UILabel alloc] initWithFrame:CommentsFrame]; 
    lblTemp.tag = 5; 
    lblTemp.numberOfLines = 1; 
    lblTemp.font = [UIFont systemFontOfSize:13.0f]; 
    lblTemp.textColor = [UIColor lightGrayColor]; 
    lblTemp.backgroundColor = [UIColor whiteColor]; 
    lblTemp.shadowColor = [UIColor whiteColor]; 
    lblTemp.shadowOffset = CGSizeMake(0, 1); 
    [cell.contentView addSubview:lblTemp]; 

    UIImageView * imgTemp = [[UIImageView alloc] initWithFrame:PinFrame]; 
    imgTemp.tag = 3; 
    [cell.contentView addSubview:imgTemp]; 

    UIView * viewTemp = [[UIView alloc] initWithFrame:ViewFrame]; 
    viewTemp.tag = 4; 
    [viewTemp setBackgroundColor:[UIColor whiteColor]]; 
    [cell.contentView addSubview:viewTemp]; 

    return cell; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * CellIdentifier = @"Cell"; 

    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    //UITableViewCell * cell = [self getCellContentView:CellIdentifier]; 

    if (cell == nil) { 
     cell = [self getCellContentView:CellIdentifier]; 
    } else { 
     [cell.contentView viewWithTag:999]; 
    } 

    cell.backgroundColor = [UIColor clearColor]; 

    UILabel * title   = (UILabel *)[cell viewWithTag:1]; 
    UILabel * journey  = (UILabel *)[cell viewWithTag:2]; 
    UIImageView * pin  = (UIImageView *) [cell viewWithTag:3]; 
    UILabel * comments  = (UILabel *)[cell viewWithTag:5]; 

    title.text = [NSString stringWithFormat:@"%@", [[feed objectAtIndex:indexPath.row] objectForKey:@"comment"]]; 
    [title sizeToFit]; 
    title.frame = CGRectMake(title.frame.origin.x, title.frame.origin.y, 240, title.frame.size.height); 

    if ([[feed objectAtIndex:indexPath.row] objectForKey:@"weather_desc"] != [NSNull null] || 
     [[feed objectAtIndex:indexPath.row] objectForKey:@"weather_temp"] != [NSNull null]) { 
      journey.text = [NSString stringWithFormat:@"It's %@ and %@°C.", [[feed objectAtIndex:indexPath.row] objectForKey:@"weather_desc"], [[feed objectAtIndex:indexPath.row] objectForKey:@"weather_temp"]]; 
    } else { 
     journey.text = [NSString stringWithFormat:@"%@", [[[feed objectAtIndex:indexPath.row] objectForKey:@"journey"] objectForKey:@"name"]]; 
    } 
    journey.frame = CGRectMake(journey.frame.origin.x, 10 + title.frame.size.height, 240, 15); 

    if ([[feed objectAtIndex:indexPath.row] objectForKey:@"latitude"] != [NSNull null]) { 
     pin.image = [UIImage imageNamed:@"LocationPin.png"]; 
    } else { 
     pin.image = [UIImage imageNamed:@"Pin.png"]; 
    } 

    if ([[feed objectAtIndex:indexPath.row] objectForKey:@"image_file_size"] != [NSNull null]) { 
     NSString * text = [NSString stringWithFormat:@"%@", [[feed objectAtIndex:indexPath.row] objectForKey:@"comment"]]; 
     CGFloat height = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(250, 1500) lineBreakMode:UILineBreakModeWordWrap].height; 

     UIImage * myImage = [images objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]]; 

     UIButton * button = [[UIButton alloc] initWithFrame:CGRectZero]; 
     [button setBackgroundImage:myImage forState:UIControlStateNormal]; 
     [button setFrame:CGRectMake(title.frame.origin.x + 55.0f, 40.0f + height, 250.0f, myImage.size.height)]; 
     [button setTag:10 + indexPath.row]; 
     [button addTarget:self action:@selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell addSubview:button]; 

     UIView * backView = (UIView *) [cell viewWithTag:4]; 
     [backView setFrame:CGRectMake(title.frame.origin.x + 50.0f, 35.0f + height, 260.0f, myImage.size.height + 10.0f)]; 
    } else { 
     UIView * backView = (UIView *) [cell viewWithTag:4]; 
     [backView setFrame:CGRectZero]; 
     [backView setBackgroundColor:[UIColor clearColor]]; 
    } 

    comments.text = [NSString stringWithFormat:@"%i", [[[feed objectAtIndex:indexPath.row] objectForKey:@"comments"] count]]; 

    UIImage * background = [UIImage imageNamed:@"CellBackground.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:background]; 
    imageView.contentMode = UIViewContentModeScaleToFill; 
    [cell setBackgroundView:imageView]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    MilestoneView * milestoneView = [[MilestoneView alloc] initWithNibName:@"MilestoneView" bundle:nil]; 
    [milestoneView setMilestone:[feed objectAtIndex:indexPath.row]]; 
    [self.navigationController pushViewController:milestoneView animated:YES]; 
} 

- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString * text = [NSString stringWithFormat:@"%@", [[feed objectAtIndex:indexPath.row] objectForKey:@"comment"]]; 
    CGFloat height = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(240, 1500) lineBreakMode:UILineBreakModeWordWrap].height; 

    if ([[feed objectAtIndex:indexPath.row] objectForKey:@"image_file_size"] != [NSNull null]) { 
     UIImage * myImage = [images objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]]; 
     height += myImage.size.height + 15.0f; 
    } 

    return height + 37; 
} 

我也在iOS 5.0中使用Apple的內置'ARC'。

親切的問候,

多米尼克

+0

說實話,你應該只使用一個NIB並做好它。這是太多的代碼來有效地管理和維護。 – 2012-01-17 23:34:12

回答

0

端固定起來通過執行以下操作簡單的代碼的解決方案:

NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview]superview]]; 

我用UIButton的發送者獲取,這是內部的細胞。我還抽出了.tag的所有修改,並將它們保留在相同的編號。

4

在這段代碼中你的cellForRowAtIndexPath添加按鈕,因此這是發生....
所以,你添加代碼按鈕,backView在getCellContentView並獲取圖像來自getCellContentView中的數組。

UIImage *myImage = [images objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]];

然後,

UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero]; 
[button setBackgroundImage:myImage forState:UIControlStateNormal]; 
[button setFrame:CGRectMake(55.0f, 40.0f, 250.0f, myImage.size.height)];

變化高度,寬度&的x,y座標或。你想要什麼和 設置單元格索引到按鈕。

[button setTag:indexPath.row]; 
[button addTarget:self action:@selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:button]; 

讓我知道如果你有任何問題....

替換下面的函數頭

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier { 
. 
. 
}

使用此功能

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier indexPathForButtonTag:(NSIndexPath*)indexPath { 
. 
. 
} 

在cellForRowAtIndex

替換以下行
cell = [self getCellContentView:CellIdentifier];

隨着

cell = [self getCellContentView:CellIdentifier indexPathForButtonTag:indexPath];
+0

唯一的問題是按鈕標籤的改變取決於它所在的行。 – Domness 2012-01-18 23:47:50

1

你不需要每個按鈕有不同的標籤。您不應該在cellForRowAtIndexPath中的單元格中添加任何子視圖,因爲您不可避免地最終會在子視圖頂部添加子視圖。我注意到你也在這個方法中添加了一個圖像視圖作爲單元格的背景 - 當你創建一個新的單元格時,你只應該這樣做。

我會假設你沒有爲這個單元格佈局使用xib文件,所以只需在contentView方法中添加按鈕並給它一個已知標籤。

在您的按鈕的操作方法,你可以找出表的按鈕是通過使用locationInView:indexPathForRowAtPoint:方法哪一行:

CGPoint hit = [sender locationInView:self.tableView]; 
NSIndexPath *hitRow = [self.tableView indexPathForRowAtPoint:hit]; 

另外請注意,視圖的.hidden屬性設置爲YES可能將其框架設置爲CGRectZero的更少的努力。