【问题标题】:Custom prototype cell xcode 6自定义原型单元 xcode 6
【发布时间】:2014-11-03 09:22:28
【问题描述】:

早上好,

我想为我的原型单元创建一个自定义设计,但我不知道该怎么做。我尝试遵循一些指南或教程,但我总是遇到一些错误,并且有些事情我做得不对。

目前我正在使用默认的原型单元格来显示以下内容:

http://i.stack.imgur.com/TnYGZ.png

现在我想插入一个 UIImageView,以及 2 个 UILabel(类似于 Facebook)以及作者和原型单元格中显示的图像。

我该怎么做?你能告诉我一些例子或一个很好的教程吗?

提前致谢。

【问题讨论】:

    标签: ios objective-c xcode xcode6


    【解决方案1】:

    为实现您的目标而要做的事情的简要清单是:

    1) 创建UITableViewCell 的Cocoa Touch Class File 子类

    2) 点击故事板,将单元格的类更改为您创建的这个

    3) 为您添加到单元格的视图(UILabel、UIImageView 等)创建 IBOutlets

    4) 然后在cellForRowAtIndexPath 下,将您的 customCell 出列并将数据传递给您的视图

    例如:

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"yourCellIdentifier" forIndexPath:indexPath];
    cell.yourImageView.image = [UIImage imageNamed:@"yourImage"];
    cell.yourLabel.text = @"Your text";
    

    这些是配置单元的基本步骤,但您可以在此处学习本教程,因为您是新的 iOS 开发人员,视频教程对您来说更方便。

    Creating a Custom UITableViewCell

    【讨论】:

    • 感谢您的评论。
    【解决方案2】:

    查看this 教程了解如何以不同方式制作自定义单元格,this 从如何使用情节提要开始详细讨论。

    【讨论】:

    • 感谢您的评论。
    【解决方案3】:

    eridb 使用 IBOutlet 向您解释。如果您想以编程方式执行此操作;

    1-) 创建新的 UITableViewCell 类。 (例如 NewTableViewCell)

    2-) 在 NewTableViewCell.h 中创建您的属性

    3-) 像这样在 NewTableViewCell.m 中创建和自定义您的属性;

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self)
        {
            // create label, uiimageview, etc. here
        }
        return self;
    }
    

    4-) 好的,您的自定义 UITableViewCell 类已准备就绪。现在,在任何你想使用的地方调用它(例如 NewTableViewController);

    #import "NewTableViewCell.h"
    

    5-) 像这样在 cellForRowAtIndexPath 中配置您的单元格;

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *reuseIdentifier = @"myTableViewID";
    
        NewTableViewCell *cell = (NewTableViewCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
        if(cell == nil)
        {
            cell = [[NewTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
        }
    
        cell.yourCustomLabel.text = @"Label-1 text";
        cell.yourCustomImageView.image = [UIImage imageNamed:@"test"];
        return cell;
    }
    

    【讨论】:

    • 感谢您的评论。
    猜你喜欢
    • 2014-01-17
    • 2014-09-28
    • 2014-08-05
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    相关资源
    最近更新 更多