Objective c 如何在ios中为Tableview创建动态TableCell
我想要达到的是这样的目标。这是一种Facebook或twitter的功能,你有一个tableview,其中有一个线程,该线程包含不同数量的文章。每行文章的数量各不相同。因此,基本上就像我在facebook上发了一篇帖子,人们对这篇帖子做出了回应,这些帖子被添加到了那个特定的帖子下(我只是关心如何显示它,其他的一切都被注意到了)。下面是图片 我知道如何创建单元格,但我不知道如何动态设置其大小。任何教程或任何建议,以实现这一点 谢谢你的帮助Objective c 如何在ios中为Tableview创建动态TableCell,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我想要达到的是这样的目标。这是一种Facebook或twitter的功能,你有一个tableview,其中有一个线程,该线程包含不同数量的文章。每行文章的数量各不相同。因此,基本上就像我在facebook上发了一篇帖子,人们对这篇帖子做出了回应,这些帖子被添加到了那个特定的帖子下(我只是关心如何显示它,其他的一切都被注意到了)。下面是图片 我知道如何创建单元格,但我不知道如何动态设置其大小。任何教程或任何建议,以实现这一点 谢谢你的帮助 谢谢您可以将其用于动态单元格高度 -(CGFloat)ta
谢谢您可以将其用于动态单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
您可以使用switch cases并选中indexPath.row和indexPath.section以返回所需的高度您可以将其用于动态单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
您可以使用switch cases并选中indexPath.row和indexPath.section以返回所需高度使用自定义UITableViewCell, 在.h文件中
@interface PartnerCell : UITableViewCell {
UILabel *user_name,*lbldate,*lbldesc;
LoadImage *img_trade;
UIImageView *partnerimage;
}
@property (nonatomic, strong) UILabel *user_name,*lbldate,*lbldesc;
@property (nonatomic, strong) LoadImage *img_trade;
@property (nonatomic, strong) UIImageView *partnerimage;
@end
在.m文件中
#导入“PartnerCell.h”
在主表视图类中编写此代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"%@",[[Partarray objectAtIndex:indexPath.row-1] valueForKey:@"part_id"]];
cell = (PartnerCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil)
{
cell = [[PartnerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[Partarray objectAtIndex:indexPath.row]] autorelease];
cell.selectionStyle= UITableViewCellSelectionStyleGray;
cell.backgroundColor = [UIColor clearColor];
cell.user_name.frame=CGRectMake(75,8,340,15);
cell.lbldate.frame=CGRectMake(75,28,340,15);
cell.lbldesc.frame=CGRectMake(75,45,340,35);
cell.user_name.text = @"user name";
cell.lbldate.text = @"date";
cell.lbldesc.text = @"description";
}
return cell;
}
获取条件并根据该条件添加对象数。获取自定义UITableViewCell, 在.h文件中
@interface PartnerCell : UITableViewCell {
UILabel *user_name,*lbldate,*lbldesc;
LoadImage *img_trade;
UIImageView *partnerimage;
}
@property (nonatomic, strong) UILabel *user_name,*lbldate,*lbldesc;
@property (nonatomic, strong) LoadImage *img_trade;
@property (nonatomic, strong) UIImageView *partnerimage;
@end
在.m文件中
#导入“PartnerCell.h”
在主表视图类中编写此代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"%@",[[Partarray objectAtIndex:indexPath.row-1] valueForKey:@"part_id"]];
cell = (PartnerCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil)
{
cell = [[PartnerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[Partarray objectAtIndex:indexPath.row]] autorelease];
cell.selectionStyle= UITableViewCellSelectionStyleGray;
cell.backgroundColor = [UIColor clearColor];
cell.user_name.frame=CGRectMake(75,8,340,15);
cell.lbldate.frame=CGRectMake(75,28,340,15);
cell.lbldesc.frame=CGRectMake(75,45,340,35);
cell.user_name.text = @"user name";
cell.lbldate.text = @"date";
cell.lbldesc.text = @"description";
}
return cell;
}
接受条件并根据该条件添加对象数。也许这会对您有所帮助。我向你们发送我的代码,我使用的是一个自定义单元格,第一个索引上有一个Emial,第二个索引上有一个电话号码,第三个索引上有一个地址(在你们的案例文章中)。我正在动态更改CellForRowatineXpath、、中地址标签的高度,以及HeightForRowatineXpath方法中的单元格高度。这是我的密码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = @"FeedCell";
FeedDetailCell *cell = (FeedDetailCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
cell = (FeedDetailCell*)[[[NSBundle mainBundle] loadNibNamed:@"FeedDetail" owner:nil options:nil] objectAtIndex:0];
}
[tableView setSeparatorColor:[UIColor grayColor]];
switch (indexPath.section) {
case 0:
if (indexPath.row == 0) {
cell.nameLabel.text = @"Telephone";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"mobile"]];
}
else {
cell.nameLabel.text = @"Mobile";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"iPhone"]];
}
break;
case 1:
cell.nameLabel.text = @"E-mail";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"Email"]];
break;
case 2:
cell.nameLabel.text = @"address";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"address"]];
CGSize size = [[_feedDictionary valueForKey:@"address"] sizeWithFont:[UIFont systemFontOfSize:14.0]
constrainedToSize:CGSizeMake(200.0, 400.0) lineBreakMode:UILineBreakModeWordWrap];
CGRect frm = cell.detailLabel.frame;
frm.size.height = size.height;
[cell.detailLabel setFrame:frm];
default:
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 2) {
NSString *address = [_feedDictionary valueForKey:@"address"];
CGSize recommendedSize = [address sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(320, INT_MAX)];
return 44 + recommendedSize.height;
}
else {
return 44;
}
}
也许这会对你有所帮助。我向你们发送我的代码,我使用的是一个自定义单元格,第一个索引上有一个Emial,第二个索引上有一个电话号码,第三个索引上有一个地址(在你们的案例文章中)。我正在动态更改CellForRowatineXpath、、中地址标签的高度,以及HeightForRowatineXpath方法中的单元格高度。这是我的密码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = @"FeedCell";
FeedDetailCell *cell = (FeedDetailCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
cell = (FeedDetailCell*)[[[NSBundle mainBundle] loadNibNamed:@"FeedDetail" owner:nil options:nil] objectAtIndex:0];
}
[tableView setSeparatorColor:[UIColor grayColor]];
switch (indexPath.section) {
case 0:
if (indexPath.row == 0) {
cell.nameLabel.text = @"Telephone";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"mobile"]];
}
else {
cell.nameLabel.text = @"Mobile";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"iPhone"]];
}
break;
case 1:
cell.nameLabel.text = @"E-mail";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"Email"]];
break;
case 2:
cell.nameLabel.text = @"address";
[cell.detailLabel setText:[_feedDictionary valueForKey:@"address"]];
CGSize size = [[_feedDictionary valueForKey:@"address"] sizeWithFont:[UIFont systemFontOfSize:14.0]
constrainedToSize:CGSizeMake(200.0, 400.0) lineBreakMode:UILineBreakModeWordWrap];
CGRect frm = cell.detailLabel.frame;
frm.size.height = size.height;
[cell.detailLabel setFrame:frm];
default:
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 2) {
NSString *address = [_feedDictionary valueForKey:@"address"];
CGSize recommendedSize = [address sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(320, INT_MAX)];
return 44 + recommendedSize.height;
}
else {
return 44;
}
}
谢谢你的帮助,但是你能详细解释一下吗?或者你能推荐一个我可以遵循的示例教程吗?谢谢你的帮助,但是你能详细解释一下吗?或者你能推荐一个我可以遵循的示例教程吗。