在滾動視圖的解決方案中,您無法在滾動視圖中滾動,因爲gestureRecognizer'獲取'觸摸。因此我根本不會使用scrollview。
使標籤尺寸調整到其內容,如:
CGSize customTextLabelSize = [cell.customTextLabel.text sizeWithFont:cell.customTextLabel.font constrainedToSize:CGSizeMake(cell.customTextLabel.frame.size.width, 999999)];
cell.customTextLabel.frame = CGRectMake(cell.customTextLabel.frame.origin.x, cell.customTextLabel.frame.origin.y, cell.customTextLabel.frame.size.width, customTextLabelSize.height);
您還需要實現這個在heightForRowAtIndexPath
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGSize cellSize = [bigTextString sizeWithFont:customTextLabel.font constrainedToSize:CGSizeMake(generalCellWidth, 999999)];
return cellSize.height;
}
這種方式,您可以只使用didSelectRowAtIndex方法。
如果你真的想用滾動視圖,添加一個按鈕,在你的cellForRowAtIndexPath細胞:方法。使按鈕就那麼大的電池,並添加一個按鈕,標籤是這樣的:
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
cellButton.tag = indexPath.row;
[cellButton addTarget:self action:@selector(cellButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellButton];
然後加:
-(void)cellButtonAction:(UIButton*)sender
{
//do something with sender.tag
}