Objective c 使用具有不同布局的多个动态单元
我在tableview中使用了这个解决方案,我的单元格只包含文本,单元格还包含图像,所以我想使用一个带有故事板和动态单元格的解决方案来布局不同的单元格,并使用此代码更改单元格标识符Objective c 使用具有不同布局的多个动态单元,objective-c,uitableview,uistoryboard,Objective C,Uitableview,Uistoryboard,我在tableview中使用了这个解决方案,我的单元格只包含文本,单元格还包含图像,所以我想使用一个带有故事板和动态单元格的解决方案来布局不同的单元格,并使用此代码更改单元格标识符 NSString *CellIdentifier = @"Cell"; NSDictionary *object = [self.listOfStuff objectAtIndex:indexPath.row]; if ([[object objectForKey:@"type"] isEqualToString:@
NSString *CellIdentifier = @"Cell";
NSDictionary *object = [self.listOfStuff objectAtIndex:indexPath.row];
if ([[object objectForKey:@"type"] isEqualToString:@"text"]){
CellIdentifier = @"OnlyTextCell";
}else if ([[object objectForKey:@"type"] isEqualToString:@"image"]){
CellIdentifier = @"ImageTextCell";
}
我是否必须使用不同的UITableViewCell,如:
-OnlyTextCell
-ImageTextCell
OnlyTextCell *cell = (OnlyTextCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
ImageTextCell *cell = (ImageTextCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
然后返回单元格
这里缺少了一些东西,因为我也知道*第二单元不起作用
所以我得到了这样的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
TTTTimeIntervalFormatter *timeIntervalFormatter = [[TTTTimeIntervalFormatter alloc] init];
NSString *timestamp = [timeIntervalFormatter stringForTimeInterval:[[object objectForKey:@"datum"] timeIntervalSinceNow]];
if ([[object objectForKey:@"type"] isEqualToString:@"text"]){
ChatCell *cell = (ChatCell *)[tableView dequeueReusableCellWithIdentifier:@"ChatCell"];
// Send the text over to the cell.
NSLog(@"ChatCell");
cell.naamGebruiker.text = [[object objectForKey:@"fromUser"] objectForKey:@"accountName"];
cell.timeLabel.text = timestamp;
cell.gebruikerImage.layer.masksToBounds = YES;
PFFile *thumbnail = [[object objectForKey:@"fromUser"] objectForKey:@"facebookImage"];
cell.gebruikerImage.image = [UIImage imageNamed:@"no-image"];
cell.gebruikerImage.file = thumbnail;
[cell.gebruikerImage loadInBackground];
cell.gebruikerImage.layer.cornerRadius = cell.gebruikerImage.frame.size.height /2;
cell.gebruikerImage.layer.masksToBounds = YES;
cell.gebruikerImage.layer.borderWidth = 0;
cell.chatBerichtTekst.text = [object objectForKey:@"tekst"];
cell.chatBerichtTekst.numberOfLines = 0;
[cell.chatBerichtTekst setLineBreakMode:NSLineBreakByWordWrapping];
[cell sizeToFit];
return cell;
} else if ([[object objectForKey:@"type"] isEqualToString:@"image"]){
ChatImageCell *cell = (ChatImageCell *)[tableView dequeueReusableCellWithIdentifier:@"ChatImageCell"];
// Send the image over the cell.
NSLog(@"ChatImageCell");
cell.naamGebruiker.text = [[object objectForKey:@"fromUser"] objectForKey:@"accountName"];
cell.timeLabel.text = timestamp;
cell.gebruikerImage.layer.masksToBounds = YES;
PFFile *thumbnail = [[object objectForKey:@"fromUser"] objectForKey:@"facebookImage"];
cell.gebruikerImage.image = [UIImage imageNamed:@"no-image"];
cell.gebruikerImage.file = thumbnail;
[cell.gebruikerImage loadInBackground];
cell.gebruikerImage.layer.cornerRadius = cell.gebruikerImage.frame.size.height /2;
cell.gebruikerImage.layer.masksToBounds = YES;
cell.gebruikerImage.layer.borderWidth = 0;
cell.chatBerichtTekst.text = [object objectForKey:@"tekst"];
cell.chatBerichtTekst.numberOfLines = 0;
[cell.chatBerichtTekst setLineBreakMode:NSLineBreakByWordWrapping];
[cell sizeToFit];
return cell;
}
return nil;
}
确保将情节提要的每个动态单元中的单元标识符设置为正确的OnlyTextCell和ImageTextCell。然后在您的tableView:cellForRowAtIndexPath:您应该有这样的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *object = [self.listOfStuff objectAtIndex:indexPath.row];
if ([[object objectForKey:@"type"] isEqualToString:@"text"]){
OnlyTextCell *cell = (OnlyTextCell *)[tableView dequeueReusableCellWithIdentifier:@"OnlyTextCell"];
// Send the text over to the cell.
return cell;
}else if ([[object objectForKey:@"type"] isEqualToString:@"image"]){
ImageTextCell *cell = (ImageTextCell *)[tableView dequeueReusableCellWithIdentifier:@"ImageTextCell"];
// Send the image over the cell.
return cell;
}
return nil;
}
Thnx,我在我的问题中编辑了上面的完整代码。我得到的数据中有错误,我想。。。