2015-09-04 57 views
0

我有两个单元格一个部分和另一个行。如何创建下拉表格视图使用两个自定义单元格?

这是我的代码

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return arrname.count; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    if(collapse==section) 
    { 
     return [arrDescription count]+1; 
    } 
    else 
    { 
     return 1; 
    } 

} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

// productCell =[[productScreenCell alloc]init]; 
    productCell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    productSub=[tableView dequeueReusableCellWithIdentifier:@"cell1"]; 
if(indexPath.row==0) 
{ 
    if(arrname.count>0) 
    { 
     NSString *str=[NSString stringWithFormat:[email protected]"%@",[arrimages objectAtIndex:indexPath.row]]; 

     productCell.lblCostProduct.text=[NSString stringWithFormat:@"%@",[arrcost objectAtIndex:indexPath.section]]; 
     productCell.imgProduct.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]]; 
     productCell.backgroundColor=[UIColor colorFromHexString:@"#232323"]; 
     productCell.lblProductScreen.text=[NSString stringWithFormat:@"%@",[arrname objectAtIndex:indexPath.section]]; 
     [productCell.btnClick addTarget:self action:@selector(touchup:) forControlEvents:UIControlEventTouchUpInside]; 
     productCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
    } 
} 
    else 
    { 
     productSub.lblSubProduct.text=[NSString stringWithFormat:@"%@",[arrDescription objectAtIndex:indexPath.row-1]]; 
    } 

// 

     productCell.btnClick.backgroundColor=[UIColor colorFromHexString:@"#ffc400"]; 
     productCell.btnClick.tag=indexPath.row; 
    return productCell; 

} 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ if (indexPath.row == 0 && collapse != indexPath.section) { 
      collapse = (int)indexPath.section; 
      [tableView reloadData]; 
     } 
     else 
     { 

     } 

    } 

我已经搜查了许多下拉代码片段,但一切都还不清楚,或者它应该是第三方程序,任何不要只给我简单的下拉表列表编码为了我。

回答

1

这里有几个问题:首先,“正确”的方法是使用节标题的内置功能。这种方法允许你创建并返回用作头在你的tableview任何部分一个UIView:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

这意味着然后,对于numberOfRowsInSection,你只返回一个值 - 行为的数部分,我想你说的是一个。你也有一个numberOfSections方法 - 如果只有一个部分,你也会返回一个。

然后,您的cellForRowAtIndexPath方法将只关心设置您的单个行,并且将在我提到的viewForHeaderInSection方法中设置标题。希望这可以指导你正确的方向。

+0

如果可能的话请发送代码@luke Smith –

相关问题