2015-09-04 88 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 –

相關問題