问题
Can anyone give me idea on how to define two custom cells in one UITableView? One cell should only contain text while the other contain video or image. The height also differs with each other. I need cells just like the given image below-
Here is the code am working on-
- (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;
}
}
And here is the output am getting-
回答1:
1) You can create seperate .xib file for the same.
2) You can create prototype cell also for above two cell.
回答2:
Yes you can do that. Create any number of custom cells you need, and create subclass for each like,
//typeA cell
class TypeATableViewCell: UITableViewCell {
// your custom properties and functions
}
//typeB cell
class TypeBTableViewCell: UITableViewCell {
// your custom properties and functions
}
then in cellForRowAtIndexPath functions, you can return cell based on 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
}
}
for dynamic Height, you can use iOS 8 feature, UITableViewAutomaticDimension
check out this article to know how to implement dynamic sizing cells https://www.raywenderlich.com/129059/self-sizing-table-view-cells
回答3:
You can do,like this:
Step 1: Create an empty Xib and put multiple cells in it.
Step 2: Customise cells according to your need and give each cell a unique Identifier.
Step 3: Assign your custom class to each of these cells.i.e if you have XYZ as your custom tableViewCell class then all these cells will have XYZ as their class.
Step 4: Now use that custom tableViewCell class for your tableView and use identifier to show different cells.
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;
}
回答4:
I worked with custom cell for table view more.It is very very easy.Even you have lot of custom cells.You can call that according to your sections title or cell.I will give you coding
First you need to set the height for cell
- (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);
}
}
来源:https://stackoverflow.com/questions/38565089/multiple-cutsom-cells-in-uitableview