【问题标题】:Table cell with multiple actions具有多个操作的表格单元格
【发布时间】:2013-04-25 10:25:27
【问题描述】:

我该如何完成这样的事情: a table cell with multiple actions inside the text
(链接到图片,因为我没有足够的点来粘贴图片)

我希望能够控制粗体文本的作用(例如激活 segue),并且我还希望单元格本身具有单独的操作。
这可能吗?

文本是动态的,因此字符串长度不是固定的。

非常感谢任何帮助! (顺便说一句,这是我对 StackOverflow 的第一个问题)。

【问题讨论】:

    标签: iphone xcode uitableview action


    【解决方案1】:

    看看TTTAttributedLabel,它可以处理文本内的多个链接。

    【讨论】:

    • 谢谢,它看起来很有希望。我会尝试一下,看看能不能让它做我想做的事!
    【解决方案2】:

    检查以下代码

    - (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];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    
    NSString *indexpath_str=[NSString stringWithFormat:@"%d",indexPath.row+1];
    
    NSString *ordinary_string=[NSString stringWithFormat:@"This is %d",indexPath.row+1];
    
    NSMutableAttributedString *attri_str=[[NSMutableAttributedString alloc]initWithString:ordinary_string];
    
    int last=[indexpath_str length];
    int begin=[ordinary_string length]-[indexpath_str length];
    
    
    
     [attri_str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(begin,last)];
        [attri_str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30] range:NSMakeRange(begin, last)];
    
    
    
    
    
    cell.textLabel.attributedText =attri_str;
    return cell;
    }
    

    【讨论】:

    • 谢谢。我尝试了您的代码,它使我可以按照我想要的方式设置文本样式,但我不明白如何将操作附加到 attri_str?
    • 你可以显示,这个字符串到 UITextView 并使 Textview 设置启用链接。
    • 您可以将此文本显示到 UIButton
    • NSMutableAttributedString *attri_str=[[NSMutableAttributedString alloc]initWithString:@"HELLO"]; [attri_str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(2,2)]; btn.titleLabel.attributedText=attri_str; // btn 是 UIButton
    • 谢谢! - 这是一个很好的例子。我会尝试一下,看看它是否符合我的需求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多