在子类 UITableViewCell 中动态创建子视图

发布于 2024-12-01 05:52:52 字数 3213 浏览 0 评论 0原文

我有一个自定义 UITableViewCell 类,希望线性显示图像和字符串。例如:

第 1 行:[图像 1] 字符串 1 [图像 2] 字符串 2 [图像 3]

第 2 行:[Image4] string3 [Image5]

图像的宽度不同,但我希望间距相等。我该怎么做?我尝试过操作子视图和CGRectMake,但没有成功。

此外,我使用 NSDictionary 来保存内容,并且每个单元格的图像/字符串数量不是恒定的。

我的CustomCell类:

#import "CustomCell.h"


@implementation CustomCell

@synthesize primaryLabel,secondaryLabel,image1;


-  (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
        primaryLabel = [[UILabel alloc]init];
        primaryLabel.textAlignment = UITextAlignmentLeft;
        primaryLabel.font = [UIFont systemFontOfSize:16];
        secondaryLabel = [[UILabel alloc]init];
        secondaryLabel.textAlignment = UITextAlignmentLeft;
        secondaryLabel.font = [UIFont systemFontOfSize:14];
        image1 = [[UIImageView alloc]init];

        [self.contentView addSubview:primaryLabel];
        [self.contentView addSubview:secondaryLabel];
        [self.contentView addSubview:image1];

    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect frame;
    frame= CGRectMake(0 ,5, 200, 25);
    primaryLabel.frame = frame;

    frame= CGRectMake(0 ,30, 200, 25);
    secondaryLabel.frame = frame;

    frame= CGRectMake(0, 60, 23, 20);
    image1.frame = frame;

...

我的RootViewController

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

    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // Set up the cell...
    NSDictionary *dictionary = nil;


//Search
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        dictionary = [self.filteredListContent objectAtIndex:indexPath.row];
    }
    else
    {
        dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
    }


//Original
    cell.primaryLabel.text = [dictionary objectForKey:@"Title"];    


    for (NSArray *keystroke in [dictionary objectForKey:@"Strokes"]) {


           for (int i = 0; i < 2; i++) {


               if ([(NSString *)keystroke isEqualToString:@"string1"] || [(NSString *)keystroke isEqualToString:@"string2"]) {
                 cell.secondaryLabel.text = (NSString *)keystroke; 
                 }

               else { 
            NSString *imageFilePath = [NSString stringWithFormat:@"%@.png", keystroke];
            NSLog(@"%@", imageFilePath);
            UIImage *myimage = [UIImage imageNamed:imageFilePath];

                cell.image1.image = myimage;

        }
        }
    }
    return cell;


}

...

显然这里有很多漏洞。首先,当我循环浏览字典时,我需要向右移动 CustomCell 子视图,以便可以将图像/文本放置在前一个子视图旁边。

I have a custom UITableViewCell class and would like to display images and strings linearly. For example:

Row 1: [Image1] string1 [Image2] string2 [Image3]

Row 2: [Image4] string3 [Image5]

The images have varying widths but I would like equal spacing. How would I do this? I have tried manipulating subviews and CGRectMake to no avail.

Additionally, I am using an NSDictionary to hold the content and the number of images/string is not constant for each cell.

My CustomCell class:

#import "CustomCell.h"


@implementation CustomCell

@synthesize primaryLabel,secondaryLabel,image1;


-  (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
        primaryLabel = [[UILabel alloc]init];
        primaryLabel.textAlignment = UITextAlignmentLeft;
        primaryLabel.font = [UIFont systemFontOfSize:16];
        secondaryLabel = [[UILabel alloc]init];
        secondaryLabel.textAlignment = UITextAlignmentLeft;
        secondaryLabel.font = [UIFont systemFontOfSize:14];
        image1 = [[UIImageView alloc]init];

        [self.contentView addSubview:primaryLabel];
        [self.contentView addSubview:secondaryLabel];
        [self.contentView addSubview:image1];

    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect frame;
    frame= CGRectMake(0 ,5, 200, 25);
    primaryLabel.frame = frame;

    frame= CGRectMake(0 ,30, 200, 25);
    secondaryLabel.frame = frame;

    frame= CGRectMake(0, 60, 23, 20);
    image1.frame = frame;

...

My RootViewController

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

    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // Set up the cell...
    NSDictionary *dictionary = nil;


//Search
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        dictionary = [self.filteredListContent objectAtIndex:indexPath.row];
    }
    else
    {
        dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
    }


//Original
    cell.primaryLabel.text = [dictionary objectForKey:@"Title"];    


    for (NSArray *keystroke in [dictionary objectForKey:@"Strokes"]) {


           for (int i = 0; i < 2; i++) {


               if ([(NSString *)keystroke isEqualToString:@"string1"] || [(NSString *)keystroke isEqualToString:@"string2"]) {
                 cell.secondaryLabel.text = (NSString *)keystroke; 
                 }

               else { 
            NSString *imageFilePath = [NSString stringWithFormat:@"%@.png", keystroke];
            NSLog(@"%@", imageFilePath);
            UIImage *myimage = [UIImage imageNamed:imageFilePath];

                cell.image1.image = myimage;

        }
        }
    }
    return cell;


}

...

Obviously there are a lot of holes here. Primarily, as I loop through my dictionary, I need to move my CustomCell subview right so I can place the images/text next to the previous subview.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2024-12-08 05:52:52

你走在正确的轨道上。在 UITableViewCell 子类中,您需要重写 layoutSubviews,为每个 UIImageViewUILabel 定义 CGRects手动,并将它们设置为相应视图的框架。

查看 CGRectGetMaxX。在这种情况下它将非常有用。

You are on the right track. In your UITableViewCell subclass, you will need to override layoutSubviews, define CGRects for each UIImageView or UILabel manually, and set them as the respective view's frame.

Check out CGRectGetMaxX. It will be very useful in this context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文