Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将“阅读更多”添加到标签并展开UITableViewCell_Ios_Swift_Uitableview - Fatal编程技术网

Ios 将“阅读更多”添加到标签并展开UITableViewCell

Ios 将“阅读更多”添加到标签并展开UITableViewCell,ios,swift,uitableview,Ios,Swift,Uitableview,我正在用标签填充UITableView。如果标签中的字符数超过120,如何在标签末尾添加“阅读更多”按钮,并使UItableViewCell展开以适应新的大标签?请帮忙。(SWIFT)我认为您必须在字符串进入标签时手动调整字符串,将其转换为一个包含120个字符的NSAttributedString(带…),然后为阅读更多内容的部分添加一个NSLinkAttributeName,并添加一个虚拟URL,overridetextView应与URL交互,并将expand函数放在那里(该函数应仅使用完整字

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

我认为您必须在字符串进入标签时手动调整字符串,将其转换为一个包含120个字符的
NSAttributedString
(带…),然后为
阅读更多内容的
部分添加一个
NSLinkAttributeName
,并添加一个虚拟URL,override
textView应与URL交互,并将expand函数放在那里(该函数应仅使用完整字符串重新加载单元格)

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

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

创建一个全局
布尔
数组:

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;
}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath{
CGFloat height=420;//指定初始高度
CGRect lblFrame=CGRectMake(40,50,194,50);//分配初始帧
NSString*strText=[someArray objectAtIndex:indexPAth.row];
CGRect rect=[strText boundingRectWithSize:CGSizeMake(lblFrame.size.width,FLT_MAX)选项:NSStringDrawingUserLineFragmentOrigin | NSStringDrawingUserFontLeading上下文:nil];
if(rect.size.height>lblFrame.size.height | | rect.size.heightlblFrame.size.height | | rect.size.height

最后在
按钮上
阅读更多内容
单击事件重新加载该特定单元格。如果要展开,请更新
isRowOpen[selectedRow]=TRUE
。如果需要折叠,请再次更新
isRowOpen[selectedRow]=FALSE
。因此,它将显示您想要的效果。

只是@VRAwesome答案的Swift 3版本:

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
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
变量高度:CGFloat=333
//指定初始高度
设lblFrame=CGRect(x:CGFloat(40),y:CGFloat(50),宽度:CGFloat(194),高度:CGFloat(50))
//指定初始帧
将strText=currentChallenge.description设为NSString
设rect=strText.boundingRect(带:CGSize(宽度:lblFrame.size.width,高度:CGFloat(Float.greatestfinitemagnity)),选项:[.usesLineFragmentOrigin.usesfunctioning],上下文:nil)
如果rect.size.height>lblFrame.size.height | | rect.size.heightlblFrame.size.height | | rect.size.height
有一个非常好的高度可配置的库,我需要swift中的一些东西,所以我可以直接配置它。谢谢您的回答!你能给我提供一些细节吗?请告诉我如何在重新加载数据之前扩展标签高度?与该标签相关的约束条件是什么?如果您的单元格只有一个标签,则只需将其固定在单元格的4侧,并让其使用
UITableViewAutomaticDimension
自动计算高度,然后,您只需更改标签的字符串并调用tableView的
beginUpdate
endUpdate
即可让它更新高度。情况并非如此。我有一个标签下高度不恒定的图像。所以我不能固定4个面。没关系,自动计算也可以处理多个动态高度视图,您只需使其中一个具有不同的
内容优先权
,并将标签和图像固定在四周的4个面,您可以在此搜索有关如何自动创建tableViewCell高度的详细信息,谢谢您的详细回答。如果可能的话,我需要用swift。strSharedText从哪里来?strSharedText从哪里来?你的答案缺乏逻辑注释,而且你的算法不起作用。我所做的只是将另一个anser转换成最新版本的swift,而不做任何更改。您可以将任何注释引用到原始autho@vrawesome