2015-04-28 97 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