Xcode 4.2 UITableview自定义单元格
问题描述:
故事板上的自定义单元格有问题。我需要从名为
I have a problem with Custom cell on story board. I need to access labels from the method called
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
如何在我的代码中定义它?当我使用IBOutlet然后它可能导致错误。
How can i define it in my code? When I used IBOutlet Then It may caused the error.
那么我怎样才能访问标签,如
So how could I access the label like
cell.textlable.text ??
cell.textlable.text ??
非常感谢。
答
一个常见的解决方案是在故事板中为每个标签添加标签,然后找到标签使用 viewWithTag:
在你的代码中找到它,如下所示:
One common solution is to give each label a tag in the storyboard, and then find the label using viewWithTag:
to find it in your code, like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = blah blah blah ...
...
UILabel *myLabel = [cell viewWithTag:1];
UILabel *anotherLabel = [cell viewWithTag:2];
// etc.
}