2016-07-07 18 views
0

我遇到了增加tableView的單元格高度的問題,我在此處使用配置單元格和帶有標記的圖像視圖。雖然我在使用自定義行高的屬性窗格中的故事板上增加了高度,但它不反映在模擬器或設備中。雖然我在故事板上增加UITableViewCell高度,但在設備或模擬器上看不到

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    UIButton *detailbtn=[[UIButton alloc]initWithFrame:CGRectMake(230,96, 60, 30)]; 
    [detailbtn addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];  detailbtn.tag = indexPath.row; 
    [detailbtn setImage:[UIImage imageNamed:@"btn-detail.png"] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:detailbtn]; 

    UIButton *imageDetailbtn=[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y+10,cell.frame.size.width, cell.frame.size.height-100)]; 
    [imageDetailbtn addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];  imageDetailbtn.tag = indexPath.row; 
    [imageDetailbtn setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateHighlighted]; 
    [cell.contentView addSubview:imageDetailbtn]; 



    // [btn setTitle:@"Detail" forState:UIControlStateNormal]; 

    UIButton *likebtn=[[UIButton alloc]initWithFrame:CGRectMake(30,96, 110, 30)]; 
    [likebtn addTarget:self action:@selector(likeButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; likebtn.tag = indexPath.row; 
    [likebtn setImage:[UIImage imageNamed:@"btn-like.png"] forState:UIControlStateNormal]; 

    NSString *likes = [NSString stringWithFormat:@"%@",[self.devices valueForKey:@"total_likes"]]; 

    [likebtn setTitle:likes forState:UIControlStateNormal]; 

    [cell.contentView addSubview:likebtn]; 

    NSLog(@"sender.tag cell is % ld ",(long)detailbtn.tag); 

    // Configure Table View Cell 
    NSLog(@"indexpath is%ld",(long)indexPath.row); 
    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    // Fetch Record 
    NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    // Update Cell 
    // [cell.textLabel setText:[record valueForKey:@"clip_name"]]; 

    UIImageView* imageView = (UIImageView*)[cell viewWithTag:110]; 
    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",K_Server_imageurl,[record valueForKey:@"clip_image_path"]]]]; 

    UILabel *lbl=(UILabel*)[cell viewWithTag:111]; 

    [lbl setText:[record valueForKey:@"clip_name"]]; 

    lbl.textColor=[UIColor whiteColor]; 
} 
+1

嘗試在 - (float)tableView中設置高度:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return height_value; } –

+0

@JennyJose但它應該增加高度,如果我增加故事板上的行高。 – vicky

+0

感謝您的答案我會盡力@JennyJose – vicky

回答

2

實現的UITableViewDelegate方法的tableView:heightForRowAtIndexPath:

如果你想有一個靜態的高度,將其設置在

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return height_value; 
} 

在故事板,只要確保表的行高設置。如果只設置表格視圖單元的行高,則無關緊要。檢查的截圖 enter image description here

enter image description here

1

打開故事板,並選擇您的電池 - >轉到尺寸檢查 - >檢查行高度複選框啓用自定義高度。否則會使用默認高度。

見下圖 Select UITableViewCell In storyboard

或者

//覆蓋下面的方法

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //return float value 
    //e.g. return 56; 
    return height; 
} 

或者

您還可以設置像如果所有的行具有以下相同的高度

self.myTableView.rowHeight = 50; 
+0

複製其他人的答案並不是一個好習慣!我可以很容易地下調你的答案,但我正在請你下次嘗試! –

+0

@JennyJose我知道你是偉大的開發者。但請檢查答案的時間。我們同時添加我們的答案,兩者都不相似。如果您仍然認爲它已被複制,請繼續並按下「我的答案」。 – Mahesh

0

如果你想設置高度表視圖單元行使用

#pragma mark - UITableView Delegate Method 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 44; // It is the Default height of tableView 
    or 
    return yourHeightValue; //Whatever you want give here 
} 

從上面的編碼

  1. UITableView有一個名爲rowHeight屬性,使用它可以用它來設置所有的UITableViewCell到相同的行高;
  2. 如果您實施UITableViewDelegate協議,並且包含tableView:heightForRowAtIndexPath:方法,那麼您可以決定當前的UITableViewCell'height。在實踐中,我們總是使用委託方法來設置單元格的高度,只要每個單元格的高度可以是動態的!而且該方法優先於UITableViewrowHeight屬性!
相關問題