2015-04-28 96 views
1

我想在https://github.com/singhson/Expandable-Collapsable-TableView上做一個使用故事板的例子。我正在嘗試使用xib文件來做到這一點。我應該在cellForRowAtIndexPath中做什麼修改,以便我可以得到所需的輸出結果?我想是這樣的名稱不顯示在UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if(cell==nil) 
    { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 
    NSString *Title= [[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"Name"]; 

    [self createCellWithTitle:Title image:[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"Image name" ] indexPath:indexPath]; 
// return [self createCellWithTitle:Title image:[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"Image name"] indexPath:indexPath]; 

    return cell; 

} 

- (UITableViewCell*)createCellWithTitle:(NSString *)title image:(UIImage *)image indexPath:(NSIndexPath*)indexPath 
{ 
    NSString *CellIdentifier = @"Cell"; 
    ExpandableTableViewCell* cell = [self.menuTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UIView *bgView = [[UIView alloc] init]; 
    bgView.backgroundColor = [UIColor grayColor]; 
    cell.selectedBackgroundView = bgView; 
    cell.lblTitle.text = title; 
    cell.lblTitle.textColor = [UIColor blackColor]; 

    [cell setIndentationLevel:[[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]]; 
    cell.indentationWidth = 25; 

    float indentPoints = cell.indentationLevel * cell.indentationWidth; 

    cell.contentView.frame = CGRectMake(indentPoints,cell.contentView.frame.origin.y,cell.contentView.frame.size.width - indentPoints,cell.contentView.frame.size.height); 

    NSDictionary *d1=[self.itemsInTable objectAtIndex:indexPath.row] ; 

    if([d1 valueForKey:@"SubItems"]) 
    { 
     cell.btnExpand.alpha = 1.0; 
     [cell.btnExpand addTarget:self action:@selector(showSubItems:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    else 
    { 
     cell.btnExpand.alpha = 0.0; 
    } 
    return cell; 
} 

現在我得到它可以展開或摺疊表,但我不顯示錶視圖名稱。我應該做些什麼改變才能發揮作用?請幫忙。

+0

你可以顯示方法:'createCellWithTitle'? –

+0

此外,NSString *標題不應該以大寫開頭 –

+0

檢查我更新的代碼 – Itaws

回答

1

我的建議:

  • 使用dequeueReusableCellWithIdentifier:forIndexPath:(在情況下注意到你我以前不註冊類),而不是dequeueReusableCellWithIdentifier:因爲最後一個doesn't崩潰,如果您沒有註冊類或筆尖對於標識符,只返回nil。
  • 確保將ExpandableTableViewCell類的「lblTitle」與界面生成器中的xib正確鏈接。
  • 簽入此說明:cell.lblTitle.text = title;如果標題爲零,或者即使您的lblTitle爲零。
  • 檢查lblTitle是否超出範圍(您正在更改此行中的contentView:cell.contentView.frame = CGRectMake(indentPoints,cell.contentView.frame.origin.y,cell.contentView.frame.size.width - indentPoints,cell.contentView.frame.size.height);)
  • 你註冊ExpandableTableViewCell使用容器類:forCellReuseIdentifier:'in viewDidLoad? 希望它有幫助。
+0

我檢查了鏈接並使用了dequeueReusableCellWithIdentifier:forIndexPath。當我放置調試器並運行它時,我可以正確地得到所有的名字,但它沒有顯示出來。還有什麼我可以檢查? – Itaws

+0

你有lblTitle與你的nib文件鏈接嗎?檢查它是否爲零。 –

+0

我已將其鏈接 – Itaws