![](/img/trans.png)
[英]Change label height of custom cell within didSelectRowAtIndexPath
[英]didSelectRowAtIndexPath for Custom label Cell
我正在使用此方法將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
)。
問題是cell1.textLabel
不是您創建的標簽。 它只是單元格創建的默認標簽。
解決方案可能是:
創建自定義單元格時,在標簽上添加標簽。
比如label.tag=100;
在didSelectRow
,
使用
UILabel *lbltemp=(UILabel *)[cell1.contentView viewWithTag:100];
然后_parent.labelone.text=lbltemp.text; should
_parent.labelone.text=lbltemp.text; should
從該標簽中獲取您的文字。
它不起作用的原因是你沒有在didSelectRowAtIndexPath
引用自定義標簽。 獲取自定義標簽引用的最簡單方法是使用標記:
- (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;
}
這里([cell.contentView addSubview:label]; _)您正在創建自定義標簽並將其作為子視圖添加到單元格的內容視圖中,但是在UILabel * lbltemp = cell1.textLabel中; 您正在嘗試訪問tableview單元格的默認標簽。在標簽上添加一些標識符(如標記)並在didSelectRowAtIndexPath中訪問它。
我認為您可以使用默認標簽並拒絕此解決方法只需添加
cell.textLabel.frame = CGRectMake(10,10, 300, 30);
在Wain的答案中,重復使用單元格也更好
使用cell.indentationallevel或cell.indentationwidth
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.