2016-07-25 24 views
1

任何人都可以給我一個關於如何在一個UITableView中定義兩個自定義單元的想法嗎?一個單元應該只包含文本,而另一個包含視頻或圖像。高度也彼此不同。我需要的細胞就像如下─UITableview中的多個關節單元

enter image description here

這裏給定的圖像的代碼我的工作導通

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier1 = @"tablecell"; 
    static NSString *CellIdentifier2 = @"tablecell1"; 

    if ([self.arrayAction isEqual:@"follow"]) 
    { 
     ActivityFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     [utilities roundedLayer:cell.sub_view.layer shadow:YES]; 
     [utilities AddBorder:cell.updated_imgView]; 

     cell.userName_label.text=[[[[[[_responseArray valueForKey:@"name"]objectAtIndex:indexPath.row]stringByAppendingString:@" "]stringByAppendingString:@"("]stringByAppendingString:[[_responseArray valueForKey:@"age"]objectAtIndex:indexPath.row]]stringByAppendingString:@")"]; 
     cell.descriptionImg_label.text=[[_responseArray valueForKey:@"description"]objectAtIndex:indexPath.row]; 

     NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat: @"http://tattoosingles.net/tattoouploads/%@",[_arrayname objectAtIndex:indexPath.row]]]; 

     [cell.updated_imgView sd_setImageWithURL:url]; 
     [utilities AddBorder:cell.updated_imgView]; 

     cell.updated_imgView.layer.cornerRadius=5; 
     cell.updated_imgView.layer.masksToBounds = YES; 
     cell.userProfile_imgView.layer.mask = [self ChangeShape:cell.userProfile_imgView]; 

     NSURL *url1 = [[NSURL alloc] initWithString:[NSString stringWithFormat: @"http://tattoosingles.net/uploads/%@",[[_responseArray valueForKey:@"path"]objectAtIndex:indexPath.row]]]; 

     [cell.userProfile_imgView sd_setImageWithURL:url1]; 

     return cell; 
    } 
    else if ([self.arrayAction isEqual:@"tplike"]) 
    { 
     ActivityFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 

     [utilities roundedLayer:cell.sub_view.layer shadow:YES]; 
     [utilities AddBorder:cell.updated_imgView]; 

     cell.userName_label.text=[[[[[[_responseArray valueForKey:@"name"]objectAtIndex:indexPath.row]stringByAppendingString:@" "]stringByAppendingString:@"("]stringByAppendingString:[[_responseArray valueForKey:@"age"]objectAtIndex:indexPath.row]]stringByAppendingString:@")"]; 
     cell.descriptionImg_label.text=[[_responseArray valueForKey:@"description"]objectAtIndex:indexPath.row]; 

     NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat: @"http://tattoosingles.net/tattoouploads/%@",[_arrayname objectAtIndex:indexPath.row]]]; 

     [cell.updated_imgView sd_setImageWithURL:url]; 
     [utilities AddBorder:cell.updated_imgView]; 

     cell.updated_imgView.layer.cornerRadius=5; 
     cell.updated_imgView.layer.masksToBounds = YES; 
     cell.userProfile_imgView.layer.mask = [self ChangeShape:cell.userProfile_imgView]; 

     NSURL *url1 = [[NSURL alloc] initWithString:[NSString stringWithFormat: @"http://tattoosingles.net/uploads/%@",[[_responseArray valueForKey:@"path"]objectAtIndex:indexPath.row]]]; 

     [cell.userProfile_imgView sd_setImageWithURL:url1]; 

     return cell; 
    } 
    else 
    { 
     ActivityFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 

     cell.userName_label.text=[[[[[[_responseArray valueForKey:@"name"]objectAtIndex:indexPath.row]stringByAppendingString:@" "]stringByAppendingString:@"("]stringByAppendingString:[[_responseArray valueForKey:@"age"]objectAtIndex:indexPath.row]]stringByAppendingString:@")"]; 
     cell.descriptionImg_label.text=[[_responseArray valueForKey:@"description"]objectAtIndex:indexPath.row]; 


     // Configure cell 

     return cell; 
    } 
} 

,這裏是輸出上午getting-

+1

請注意我對你的問題作出的修改。你的代碼不可讀,請保留格式,以便其他人可以回答你的問題 –

+0

好的。我會牢記! – pri

+0

我的答案是否適合你? – user3182143

回答

0

1)您可以爲此創建單獨的.xib文件。

2)您也可以爲上述兩個單元格創建原型單元格。

+0

我已經完成了第二步,但我的代碼無法正常工作。你能檢查我的代碼嗎? – pri

+0

你的代碼在哪裏? –

+0

我上面已經編輯過。 – pri

0

是的,你可以做到這一點。創建任意數量的需要定製的細胞,併爲每個像創建子類,

//typeA cell 
class TypeATableViewCell: UITableViewCell { 
    // your custom properties and functions 
} 
//typeB cell 
class TypeBTableViewCell: UITableViewCell { 
    // your custom properties and functions 
} 

然後在的cellForRowAtIndexPath功能,可以返回基於indexPath細胞,

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    // here i used section to return different cell, but it can be anything 
    if indexPath.section == 0 { 

     let cell = tableView.dequeueReusableCellWithIdentifier("TypeATableViewCell", forIndexPath: indexPath) as! TypeATableViewCell 

     // your custom code 
     return cell 

    }else if indexPath.section == 1 { 

     let cell = tableView.dequeueReusableCellWithIdentifier("TypeBTableViewCell", forIndexPath: indexPath) as! TypeBTableViewCell 

     // your custom code 
     return cell 
    } 
} 

動態身高,你可以使用iOS 8功能,UITableViewAutomaticDimension

查看本文知道如何實現動態調整大小的單元格https://www.raywenderlich.com/129059/self-sizing-table-view-cells

0

你可以這樣做,是這樣的:

步驟1:創建一個空XIB,並把多個細胞在裏面。

第2步:根據您的需要,並給每個單元的唯一標識符自定義單元格。

步驟3將您的自定義類的每個這些細胞 .i.e的,如果你有XYZ作爲您的自定義tableViewCell類,那麼所有這些細胞就會有XYZ作爲其類。

第4步:現在使用自定義的tableViewCell類爲您的tableView和使用的標識符顯示不同的細胞。

static NSString * CellIdentifier = @"";//Use_Identifier_Here 
NSInteger indexForCell = 0; 
     indexForCell = [YOUR_CUSTOM_CELL nibIndexForIdentifier:CellIdentifier]; 
     cell= (YOUR_CUSTOM_CELL *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      NSString *strNib = @"YOUR_CUSTOM_CELL"; 
      NSArray *nib = 
      [[NSBundle mainBundle] loadNibNamed:strNib owner:self options:nil]; 
      cell = (YOUR_CUSTOM_CELL *)[nib objectAtIndex:indexForCell]; 
     } 

nibIndexForIdentifier:

+(NSInteger)nibIndexForIdentifier:(NSString *)cellIdentifier 
{ 
    NSInteger nibIndex =0; 
    if ([cellIdentifier isEqualToString:@"YOUR_FIRST_CELL_IDENTIFIER"]) { 
     nibIndex =0; 
    }else if ([cellIdentifier isEqualToString:@"YOUR_SECOND_CELL_IDENTIFIER"]){ 
     nibIndex = 1; 
    } 

    return nibIndex; 
} 
+0

謝謝!!但是,如果我不想使用xib文件呢? – pri

+0

然後,您將不得不爲您的單元格數創建單獨的自定義單元格。通過這樣做,您將不得不通過使用標識符訪問它們。這種方法會增加你的文件數量,並且你將不得不在不同的地方進行編碼,而這些地方可能很難管理。 –

0

我與自定義單元格的工作表視圖more.It是非常非常easy.Even你有很多定製的cells.You可以根據自己的部分調用標題或單元格。我會給你的編碼

首先,您需要設置高度爲電池

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([[self.arrayAction objectAtIndex:indexPath.section] isEqualToString:@"follow"]) 
     return 60; //This is for Label text 
    return 100; //This is for image and video 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ActivityFeedCell *cell = (ActivityFeedCell *)[tableView dequeueReusableCellWithIdentifier:@"Reuse"]; 
    NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"ActivityFeedCell" owner:self options:nil]; 
    @try 
    { 
    if ([[self.arrayAction objectAtIndex:indexPath.section] isEqualToString:@"follow"]] 
    { 
     if(cell == nil) 
      cell = [nib objectAtIndex:0; 
     [email protected]""; //Your Value 
     [email protected]"";//Your Value 

    } 
    else if ([[self.arrayAction objectAtIndex:indexPath.section] isEqualToString:@"tplike"]] 
    { 
     if(cell == nil) 
      cell = [nib objectAtIndex:1]; 
     cell.updated_imgView.image = [UIImage imageNamed:@""];//Your Image or Video 
    } 

    return cell; 
    } 
    @catch (NSException *exception) 
    { 
    NSLog(@"%@",exception); 
    } 
} 
+0

am在這一行中遇到錯誤--- NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@「ActivityFeedCell」owner:self options:nil];你能告訴我爲什麼嗎? – pri

+0

你必須給你的CustomCell Name.That是像CustomCell.xib或ActivityFeedCell.xib的Nib – user3182143

+0

是你的自定義單元名'ActivityFeedCell'? – user3182143

相關問題