表视图单元格,如Notes App

时间:2017-03-04 23:13:00

标签: ios uitableview tableviewcell

通过我的搜索,我无法找到答案。

基本上我有一个基本的注释,如视图控制器。我希望像笔记这样的效果,其中第一行是音符的标题,而正文的其余部分则在字幕尾随。

我如何设置:

NSString *note = nil;
if (tableView == self.tableView) {
    note = [noteArray objectAtIndex:indexPath.row];
}
NSString *date = [dateArray objectAtIndex:indexPath.row];
NSInteger charnum = [note length];
if (charnum >= 22) {
    cell.textLabel.text = [[note substringToIndex:18] stringByAppendingString:@"..."];
}
else{
    cell.textLabel.text = note;
}

[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ - %@", date, [note substringFromIndex:18]]];
[cell.detailTextLabel setNumberOfLines:1];

现在它会读取18个字符,然后......然后字幕会为其余字符选择它。

我如何为此设置标题/标题然后设置正文?

1 个答案:

答案 0 :(得分:0)

所以我明白了。如果有人对此感兴趣,我就是这样做的。

这是在textView中正确设置的组合,如下所示:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    NSMutableString *newString = [NSMutableString stringWithString:self.textView.text];
    [newString replaceCharactersInRange:range withString:text];
    NSString *trimmed = [newString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    NSRange newLineRange = [trimmed rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
    if(newLineRange.length > 0)
    {
        self.textView.text = [trimmed substringToIndex:newLineRange.location];
    }
    else {
        self.textView.text = trimmed;
    }

    return YES;

}

将第一行保存为"标题"

接下来在我的tableView中,我将显示设置如下:(我将提供整个单元格调用,而不是我更改的内容)。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    [cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
    [cell.textLabel setNumberOfLines:1];
    [cell.textLabel setLineBreakMode : NSLineBreakByTruncatingTail];
    [cell setClipsToBounds:YES];

    // Configure the cell label.
    [cell.textLabel setTextColor: [UIColor whiteColor]];
    [cell.textLabel setFont:[UIFont boldSystemFontOfSize:18]];

    //Cell Base color
    [cell.contentView.superview setBackgroundColor:[UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1]];

    //Cell selection color
    UIView *cellColor = [[UIView alloc] init];
    [cellColor setBackgroundColor:[UIColor colorWithRed:0.176 green:0.176 blue:0.176 alpha:0.95]];
    [cell setSelectedBackgroundView:cellColor];

    //Cell border
    [cell.layer setBorderColor:[UIColor colorWithRed:30/255.0 green:30/255.0 blue:30/255.0 alpha:1.0].CGColor];
    [cell.layer setBorderWidth:1.5f];

    //Setting the first line of cell.
    NSString *note = nil;
    if (tableView == self.tableView) {
        note = [noteArray objectAtIndex:indexPath.row];
    }
    cell.textLabel.text = note;

    //Subtitle line.
    NSRange startRange = NSMakeRange(1, 0);
    NSRange titleRange = [note lineRangeForRange:startRange];
    NSString *titleString = [note substringFromIndex:titleRange.length];
    titleString = [titleString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    //titleString = [titleString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];

    //Setting the date.
    NSString *date = [dateArray objectAtIndex:indexPath.row];

    [cell.detailTextLabel setNumberOfLines:1];

    //New method of having two seperate colors for the detail string.
    NSAttributedString *scoreAttributedString = [[NSAttributedString alloc] initWithString:date attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0.20 green:0.67 blue:0.86 alpha:1.0]}];
    NSAttributedString *domainAttributedString = [[NSAttributedString alloc] initWithString:titleString attributes:@{NSForegroundColorAttributeName: [UIColor lightTextColor]}];

    NSMutableAttributedString *detailMutableAttributedString = [[NSMutableAttributedString alloc] init];
    [detailMutableAttributedString appendAttributedString:scoreAttributedString];
    [detailMutableAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    [detailMutableAttributedString appendAttributedString:domainAttributedString];

    [[cell detailTextLabel] setAttributedText:detailMutableAttributedString];  
}

希望这有助于某人。