【问题标题】:Adding multiple icons in a cell of a UITableViewCell在 UITableViewCell 的单元格中添加多个图标
【发布时间】:2012-08-31 19:49:07
【问题描述】:

我有一个 UITableView,其中包含单元格,并且表格视图右侧有按钮,因此当我单击其中一个按钮时,它会获取所选 TableViewCell 的标签文本,并将其保存到特定于被点击的按钮。这些按钮有不同颜色的文本,“绿色、蓝色、红色、紫色和橙色”。我想要发生的是,当我单击其中一个按钮时,它会在单元格中的文本右侧放置一个彩色圆圈。圆圈的颜色将取决于按下的按钮。我觉得你可以用单元格的附件视图做一些事情,但棘手的是,当你点击同一个单元格的另一个按钮时,它会放入另一个彩色圆圈。如果需要,我希望能够显示 5 个圆圈并且如果您再次单击相同的按钮,它将带走您之前添加的圆圈。这是我的应用程序的图片,上面有一些经过 Photoshop 处理的圆圈,可以给你一个想法。

有没有人对这个问题有任何想法或方法。我似乎无法弄清楚您将如何在附件视图中设置多个圆圈,以及它将如何影响文本。它会移动文本吗?

我们将不胜感激。

谢谢

【问题讨论】:

  • 您是否准备好自定义视图以用作单元格附件视图?
  • 是的,我可以点击,它会在配件的右侧打上一个复选标记。
  • 你需要做的就是为这个自定义视图添加切换 5 个圆圈可见性的方法,同时实现 layoutSubviews 以很好地显示它们。
  • 一次可以拥有多个表格视图单元格附件吗?
  • 您将拥有一个附属视图,并且您可以随意自定义它,例如添加 5 个子视图。

标签: objective-c ios xcode


【解决方案1】:

你试试这个代码:

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

// 在您的情况下,它必须是此处的自定义单元格对象。

MyCustomCell *cell = (MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"AnIdentifierString"];

    if (cell == nil) {
    cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AnIdentifierString"] autorelease];
}

//use your cell here----------

//  cell.textLabel.text = @"This text will appear in the cell";


    return cell;
}

您可以使用 MyCustomCell 的样式覆盖 init,如下所示为多个图像

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

//change frame sizes to palce the image in the cell
    UIImageView * thumbnail1 = [[UIImageView alloc] initWithFrame:CGRectMake(17, 12, 62, 62)];
    thumbnail1.image = [UIImage imageNamed:@"Icon.png"];
    [self addSubview:thumbnail1];

    UIImageView * thumbnail2 = [[UIImageView alloc] initWithFrame:CGRectMake(17, 12, 62, 62)];
    thumbnail1.image = [UIImage imageNamed:@"Icon.png"];
    [self addSubview:thumbnail2];

    UIImageView * thumbnail3 = [[UIImageView alloc] initWithFrame:CGRectMake(17, 12, 62, 62)];
    thumbnail3.image = [UIImage imageNamed:@"Icon.png"];
    [self addSubview:thumbnail3];

}
return self;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多