【发布时间】:2014-02-20 00:46:49
【问题描述】:
我的控制器中有这个:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PostCell";
PostCell *postCell = (PostCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
Post *post = self.displayPosts[indexPath.row];
[postCell setPost:post];
return postCell;
}
通过[postCell setPost:post];,我发送了我将要使用的自定义单元模型。
- (void)setPost:(Post *)newPost
{
if (_post != newPost) {
_post = newPost;
}
[self setNeedsDisplay];
}
现在这是我拥有的版本,但我想更改它。现在我有[self setNeedsDisplay],它调用drawRect - 我想使用subviews,但不知道如何启动它。
我在哪里添加subviews,如果使用IBOutlets,我在哪里设置子视图值(imageviews 图像,labels 文本等...)基于该_post??
或者如果更容易指出我如何使用drawRect将buttons添加到我的单元格,以及如何在drawRect内的图像和按钮上检测touch?这样我就不需要更改当前版本了。
【问题讨论】:
-
在您的
setPost函数中,您需要创建一个视图(无论您需要什么类型),然后将该视图添加为您的单元格的子视图:[self.contentView addSubview:newView]。然后对您希望单元格包含的任何其他视图再次执行此操作。
标签: ios objective-c cocoa-touch uitableview subview