2012-11-14 25 views
0

我試圖根據tableview中的細胞子視圖添加到視圖添加子視圖..我做這個代碼已經根據要UITableViewCell中的CGRect

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    VerseCell *cell = (VerseCell *)[tableView cellForRowAtIndexPath:indexPath]; 

    rowSelected = indexPath.row; 

    CGRect cellRect = cell.frame; 

    UIView *cellView =[[UIView alloc]initWithFrame:CGRectMake(cellRect.origin.x+4, cellRect.origin.y+15, cellRect.size.width-20, 70)]; 

    UIImageView* background =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"FavoritePanelBackground"]]; 
    background.frame =CGRectMake(cellRect.origin.x+4, cellRect.origin.y+15, cellRect.size.width-20, 70); 
    [cellView addSubview:background]; 
    [cellView setBackgroundColor:[UIColor redColor] ]; 
    UIButton * favoritsB = [[UIButton alloc]initWithFrame:CGRectMake(cellRect.origin.x+10, cellRect.origin.y+15, 60, 40)]; 
    [favoritsB setBackgroundImage:[UIImage imageNamed:@"MofadalatNormalIcon.png"] forState:UIControlStateNormal]; 
    [favoritsB setBackgroundImage:[UIImage imageNamed:@"MofadalatTappedIcon.png"] forState:UIControlStateHighlighted]; 
    [favoritsB addTarget:self action:@selector(favorite) forControlEvents:UIControlEventTouchUpInside]; 

    [cellView addSubview:favoritsB]; 

    cellView=nil; 

} 

問題..此代碼按照預期在第一行工作..當點擊下一個單元格時,它會顯示主UIVIew(我用紅色背景標記)在預期位置,而子視圖會非常靠後...所以我在這裏做錯了什麼?如果這不起作用,我還有另一種方法嗎?

回答

0

你在哪裏試圖添加這些視圖?在表視圖單元格內?如果是這樣,你永遠不會添加cellView作爲單元格的子視圖。如果這就是你想要做的,只需在cellView = nil上方添加這一行:

[cell addSubview:cellView];

+0

實際上,我將它添加爲self.view的子視圖而不是表本身 –