简体   繁体   English

以编程方式自定义TableViewCell

[英]Custom TableViewCell programmatically

I have source code that does not use storyboard or xib. 我有不使用情节提要或xib的源代码。 It contains a UITableView and the cells use the standard cell.textlabel. 它包含一个UITableView,并且单元格使用标准的cell.textlabel。

What i want is to use a custom TableViewCell, normally i would create a UITableViewCell class and connect them in storyboard, but i cant connect them here since its not using storyboard or xib. 我想要的是使用自定义TableViewCell,通常我会创建一个UITableViewCell类并将它们连接到情节提要中,但由于它未使用情节提要或xib,因此我无法在此处将它们连接起来。

I have following UITableViewClass 我有以下UITableViewClass

@interface chatCell : UITableViewCell
@property(nonatomic, weak) IBOutlet UIImageView *homeTeamImage;
@property(nonatomic, weak) IBOutlet UILabel *homeTeamLabel;
@property(nonatomic, weak) IBOutlet UIImageView *awayTeamImage;
@property(nonatomic, weak) IBOutlet UILabel *awayTeamLabel;
@end

How can i place them inside the TableViewCell and connect them to the properties? 如何将它们放置在TableViewCell中并将它们连接到属性?

so i can start using 所以我可以开始使用

cell.homeTeamImage
cell.homeTeamLabel
cell.awayTeamImage
cell.awayTeamLabel

You don't need to use IBOutlet, since you don't want to use the properties in IB 您不需要使用IBOutlet,因为您不想使用IB中的属性

@interface chatCell : UITableViewCell
{
    UIImageView *homeTeamImage;
    UILabel *homeTeamLabel;
    UIImageView *awayTeamImage;
    UILabel *awayTeamLabel;
}

@property(nonatomic, weak) UIImageView *homeTeamImage;
@property(nonatomic, weak) UILabel *homeTeamLabel;
@property(nonatomic, weak) UIImageView *awayTeamImage;
@property(nonatomic, weak) UILabel *awayTeamLabel;

@end

and don't forget to 而且不要忘记

@synthesize homeTeamImage, ...; 

in your implementation file. 在您的实施文件中。

You can override the - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath . 您可以覆盖-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath Instead of returning a standard UITableViewCell you can return yours 无需返回标准的UITableViewCell,您可以返回自己的

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    chatCell c = [[chatCell alloc] init];
    return c;
}

You don't need the IBOutlet keyword: it isn't a type, it's only used by xcode when you use the storyboard. 您不需要IBOutlet关键字:它不是一种类型,仅在使用情节提要时由xcode使用。 I think that without storyboard you should change the properties to strong pointers because no one else is strongly pointing to them so they'll be deleted. 我认为没有情节提要,您应该将属性更改为强指针,因为没有其他人强烈地指向它们,因此它们将被删除。

The @synthesize is not mandatory if you use xcode 5. It's required only if you override both setter and getter of a property. 如果您使用xcode 5,则@synthesize不是必需的。仅当同时覆盖属性的setter和getter时,才需要@synthesize。

See Programmatically Adding Subviews to a Cell's Content View from Table View Programming Guide for iOS. 请参阅《 iOS表格视图编程指南》中的以编程方式将子视图添加到单元格的内容视图中 In this example cells are created in tableView:cellForRowAtIndexPath: but you can use same approach (add views on cell's content view) in UITableViewCell subclass. 在此示例中,在tableView:cellForRowAtIndexPath:中创建了单元格,但是您可以在UITableViewCell子类中使用相同的方法(在单元格的内容视图上添加视图)。

@interface chatCell : UITableViewCell
{
    UIImageView *homeTeamImage;
    UILabel *homeTeamLabel;
    UIImageView *awayTeamImage;
    UILabel *awayTeamLabel;
}

@property(nonatomic, weak) UIImageView *homeTeamImage;
@property(nonatomic, weak) UILabel *homeTeamLabel;
@property(nonatomic, weak) UIImageView *awayTeamImage;
@property(nonatomic, weak) UILabel *awayTeamLabel;

@end


and don't forget to

    enter code here

@synthesize "All properties"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM