将更多内容添加到标签并展开UITableViewCell

时间:2016-09-21 10:16:30

标签: ios swift uitableview

我正在使用标签填充UITableView。只有当该标签中的字符数超过120并使UItableViewCell扩展以适应新的大标签时,如何在标签末尾添加“阅读更多”按钮?请帮忙。 (SWIFT)

3 个答案:

答案 0 :(得分:2)

我认为你必须在进入标签时手动调整字符串,将其转换为带有120个字符的NSAttributedString(带有....)然后为NSLinkAttributeName添加read more textView shouldInteractWithURL部分使用虚拟URL,覆盖$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => "https://your_url_here", CURLOPT_POST => 1, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_POSTFIELDS => http_build_query(array( "key1" => "val1", "key2" => "val2", )) )); $resp = curl_exec($curl); print_r($resp); if (!curl_exec($curl)) { die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl)); } curl_close($curl); 并在其中放置扩展函数(应该只使用完整字符串重新加载单元格)

答案 1 :(得分:1)

只是一个Swift 3版本的@VRAwesome回答:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    var height: CGFloat = 333

    // assign initial height
    let lblFrame = CGRect(x: CGFloat(40), y: CGFloat(50), width: CGFloat(194), height: CGFloat(50))

    //assign initial frame
    let strText = currentChallenge.description as NSString
    let rect = strText.boundingRect(with: CGSize(width: lblFrame.size.width, height: CGFloat(Float.greatestFiniteMagnitude)), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)

    if rect.size.height > lblFrame.size.height || rect.size.height < lblFrame.size.height {
        let diff = rect.size.height - lblFrame.size.height
        height = height + diff + 20
    }

    if isRowOpen[indexPath.row] == true {

        let rect = strSharedText.boundingRect(with: CGSize(width: lblFrame.size.width, height: CGFloat(Float.greatestFiniteMagnitude)), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)

        if rect.size.height > lblFrame.size.height || rect.size.height < lblFrame.size.height {
            let diff = rect.size.height - lblFrame.size.height;
            height = height + diff + 20;
        }
    }

    return height
}

答案 2 :(得分:0)

首先计算要分配给标签的字符串的字符。如果超过120个字符,则在单元格的右下角创建并添加按钮(Read More)。

Read More & Expand

当按Read More然后使用boundingRectWithSize计算字符串的大小,然后重新加载该特定单元格,它将更新单元格的大小。

创建一个全局boolean数组:

bool isRowOpen[];

你的方法应该是:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat height = 420; // assign initial height
    CGRect lblFrame = CGRectMake(40, 50, 194, 50); //assign initial frame

    NSString *strText = [someArray objectAtIndex:indexPAth.row];
    CGRect rect = [strText boundingRectWithSize:CGSizeMake(lblFrame.size.width, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];

    if (rect.size.height > lblFrame.size.height || rect.size.height < lblFrame.size.height) {
        float diff = rect.size.height - lblFrame.size.height;
        height = height+diff+20;
    }

    if (isRowOpen[indexPath.row] == TRUE) {

        CGRect rect = [strSharedText boundingRectWithSize:CGSizeMake(lblFrame.size.width, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];

        if (rect.size.height > lblFrame.size.height || rect.size.height < lblFrame.size.height) {
            float diff = rect.size.height - lblFrame.size.height;
            height = height+diff+20;
        }
    }
    return height;
}

最后在Button Read More点击事件重新加载该特定单元格。如果要扩展,请更新isRowOpen[selectedRow] = TRUE。如果要折叠,请再次更新isRowOpen[selectedRow] = FALSE。所以它会显示你想要的效果。