gpt4 book ai didi

ios - 在同一表格单元格中添加两个文本标签(一个动态标签和一个静态标签)

转载 作者:行者123 更新时间:2023-11-29 12:42:06 25 4
gpt4 key购买 nike

我在 View Controller 中有一个带有自定义单元格的 TableView 。我的表格 View 工作正常。我想要做的是以编程方式开发下面的图像。其中“标签”是自定义的文本,并根据某些输入而变化。如何包含这两个标签(在 cellForRowAtIndexPath: 中)并确定它们在单元格中的位置。图片包含 2 个不同的表格单元格。

Image contains 2 different table cells

我知道如何通过 Storyboard来完成,但我需要以编程方式进行,因为我在 xCode 4.5 中使用动态单元格。

图像引用了索引单元格 1 和 2。到目前为止,我只设法为每个单元格包含一个文本标签。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}




switch ([indexPath row])
{
case 0:
{
// image cell - image resize and centred


UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
imv.image=[UIImage imageNamed:@"test1.jpg"];


imv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[cell.contentView addSubview:imv];

imv.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

cell.accessoryType = UITableViewCellAccessoryNone;

break;
}
case 1:
{


cell.textLabel.text = @"Name";


break;
}

case 2:
{
cell.textLabel.text = @"Manufacturer";

break;
}

case 3:
{
cell.textLabel.text = @"Overall Score";

break;
}

case 4:
{
cell.textLabel.text = @"Description";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

break;
}

case 5:
{
cell.textLabel.text = @"Videos";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

break;
}
}


return cell;

}

提前致谢

最佳答案

先查看默认的UITableViewCell样式:UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle.

如果您可以使用它们,则不需要子类化 UITableViewCell。否则你将不得不这样做并放下 3 个属性:一个 UIView 和 2 个 UILabel。原因是如果不使用默认单元格样式,则无法移动或添加单元格元素。

您的 UITableViewCell 子类应具有以下代码:

@interface UITableViewCellSubClass : UITableViewCell
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@end

@implementation UITableViewCellSubClass
@synthesize view;
@synthesize label1;
@synthesize label2;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate label1 with position (10,10,150,20)
label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,10,150,20)];
// initiate label2 with position (170,10,150,20)
label2 = [[UILabel alloc] initWithFrame:CGRectMake(170,10,150,20)];
[view addSubview:label1];
[view addSubview:label2];
}
return self;
}
@end

然后您可以在您的 cellForRowAtIndex: 方法中返回它,基本上:

UITableViewCellSubclass *cell = [[UITableViewCellSubclass alloc] initWithStyle:UITableViewCellDefault reuseIdentifier:@"cell"];
[cell.label1 setText:@"text"];
[cell.label2 setText:@"more text"];

希望这有帮助,不要忘记#import "UITableViewCellSubClass.h"

关于ios - 在同一表格单元格中添加两个文本标签(一个动态标签和一个静态标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24613371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com