UITableview中的多个切断单元格

任何人都可以给我想法如何在一个UITableView中定义两个自定义单元格? 一个单元应该只包含文本,而另一个包含video或图像。 高度也彼此不同。 我需要像下面给出的图像一样的单元格 –

在这里输入图像说明

这是我正在开发的代码,

- (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; } } 

这里是输出越来越 –

1)您可以创build单独的.xib文件。

2)您也可以为两个以上的单元格创build原型单元格。

是的,你可以这么做。 创build您需要的任意数量的自定义单元格,并为每个类创build子类,

 //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 } } 

对于dynamic高度,您可以使用iOS 8function, UITableViewAutomaticDimension

看看这篇文章,了解如何实现dynamicresize的细胞https://www.raywenderlich.com/129059/self-sizing-table-view-cells

你可以这样做:

第1步:创build一个空的Xib,并在其中放置多个单元格。

步骤2:根据您的需要自定义单元格,并给每个单元格一个唯一的标识符

步骤3将自定义类分配给每个单元格 .ie如果您将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; } 

我使用自定义单元格为表视图more.It是非常非常容易的。即使你有很多的自定义单元格。您可以根据您的部分标题或单元格来调用它。我会给你编码

首先你需要设置单元的高度

 - (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; cell.userName_label.text=@""; //Your Value cell.descriptionImg_label.text=@"";//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); } }