我有一个自定义单元格(带有 5 个标签),用于填充我的自定义 UITableView。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//--> Need to get content of each label within the custom cell
}
感谢帮助 谢谢!
最佳答案
你可以用不同的方式做到这一点
最好的方法是使用标签
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
//get the labels using Tag from cell
UILabel *label = ([[cell.contentView viewWithTag:yourTag] isKindOfClass:[UILabel class]])?(UILabel *)[cell.contentView viewWithTag:yourTag]:nil;
}
第二种方法是使用 subview 循环。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
for(id subView in [cell.contentView subView])
{
if([subViews isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *)subViews;
}
}
}
自定义单元类示例:
UILabel *myLabel = [[UILabel alloc] init]
myLabel.tag = 1;
[self.contentView addSubView:myLabel];
关于ios - 获取自定义单元格的标签内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15376121/