我使用此方法将UILabel
添加到我的UITableView
并且其工作良好,但我还需要创建一个方法来选择行并保存到一个临时字符串中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
[cell.contentView addSubview:label];
label.text = [tableArr objectAtIndex:indexPath.row];
label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0];
return cell;
}
但它不起作用。当我使用cell时,它工作得很好。textLabel,但不带自定义标签。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
UILabel *lbltemp = cell1.textLabel;
_parent.labelone.text = lbltemp.text;
}
您可以创建自己的UITableViewCell
子类,以允许您获得对标签的引用(并防止您在单元格上创建多个标签)。或者你可以使用这样的标签:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell = nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
label.tag = 123123;
[cell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[cell viewWithTag:123123];
label.text = [tableArr objectAtIndex:indexPath.row];
label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:123123];
_parent.labelone.text = label.text;
}
您的原始代码正在添加一个新标签,但随后试图从默认标签获取文本…它也总是创建一个新的单元格,并添加一个新的标签,这是相当浪费。
另外,您通常不应该在标签中存储文本,您应该真正使用tableArr
来获取文本。当您重复使用单元格或让用户编辑文本(在UITextField
中)时,标记方法更适合更新标签。
它不起作用的原因是您没有引用didSelectRowAtIndexPath
中的自定义标签。获取自定义标签引用的最简单方法是使用tags:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
// Also, this is the proper way to use reuseIdentifier (your code is incorrect)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
[cell.contentView addSubview:label];
label.text = [tableArr objectAtIndex:indexPath.row];
label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0];
label.tag = 1;
return cell;
}
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
UILabel *customLabel = [cell viewWithTag:1];
_parent.labelone.text = customLabel.text;
}
问题是cell1.textLabel
不是您创建的标签。它只是单元格创建的默认标签。
解决方案可以是:
在创建自定义单元格时为标签添加标记。
说label.tag=100;
在didSelectRow
中
UILabel *lbltemp=(UILabel *)[cell1.contentView viewWithTag:100];
然后_parent.labelone.text=lbltemp.text; should
从标签中抓取文本
_)你正在创建你的自定义标签,并将其添加为子视图单元格的内容视图,但在UILabel *lbltemp = cell1.textLabel;你正在尝试访问tableview cell的默认标签。添加一些标识符(如标签)到你的标签和访问它在didSelectRowAtIndexPath。
我认为您可以使用默认标签并拒绝此解决方案只需添加
cell.textLabel.frame = CGRectMake(10,10, 300, 30);
对于内存来说,重用单元格也更好,就像Wain的回答
使用cell。缩进的国家级别或单元。indentationwidth