简体   繁体   English

如何在UITableViewCellStyleSubtitle中显示多行

[英]How to display multiple lines in UITableViewCellStyleSubtitle

So I have a download manager in my app. 所以我的应用程序中有一个下载管理器。 I have taken it upon myself to liven it up today. 我自负要活到今天。

I have implemented UITableViewCellStyleSubtitle, and is displaying properly. 我已经实现了UITableViewCellStyleSubtitle,并且显示正确。

I want to add more than 1 line to it. 我想添加多于1行。 Right now I'm stuck in choosing either the file size or the formatted date. 现在,我只能选择文件大小或格式化日期。

How would I do both? 我会怎么做? ie

Cell Title
Date: (followed by file size) or
File Size:

Below is the relevant code I'm working with. 以下是我正在使用的相关代码。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier] autorelease];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
        }

    // Configure the cell.
        cell.textLabel.text = [directoryContents objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryNone;
        [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];
        cell.textLabel.numberOfLines = 0;


    //Get file size
        NSError *error;
        NSString *fileName = [directoryContents objectAtIndex:indexPath.row];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"my folder"];
        path = [path stringByAppendingPathComponent:fileName];

        NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
        NSInteger fileSize = [[fileAttributes objectForKey:NSFileSize] intValue];

    //Setting the date
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"my folder"];
        filePath = [filePath stringByAppendingPathComponent:fileName];

        NSDate *creationDate = nil;
        NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
        creationDate = attributes[NSFileCreationDate];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM-dd-yyyy"];
        NSString *dateString = [dateFormatter stringFromDate:creationDate];


/////This is where I need to blend the dateString with the file size//////
        cell.detailTextLabel.text = [NSString stringWithFormat:dateString, @"%@", [self formattedFileSize:fileSize]];
        cell.detailTextLabel.numberOfLines = 2;

        return cell;
    }

Thank you in advanced. 在此先感谢您。

I tried this out, and for some reason, setting numberOfLines to 2 didn't work for me either, but setting it to anything greater then 2, or setting it to 0 worked. 我尝试了一下,由于某种原因,将numberOfLines设置为2也不适合我,但是将其设置为大于2的任何值,或者将其设置为0都可以。

You need to format your two strings properly. 您需要正确格式化两个字符串。 This is not correct syntax, 这是不正确的语法,

[NSString stringWithFormat:dateString, @"%@", [self formattedFileSize:fileSize]]

It should be like this, 应该是这样

[NSString stringWithFormat:@"%@\n%@",  dateString, [self formattedFileSize:fileSize]];

Figured this out. 想通了。 Just had to tweak around with the format of the string. 只需调整字符串的格式即可。

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@\n%@", dateString, [self formattedFileSize:fileSize]];

As posting this answer the page refreshed and seen that rdelmar is also correct in his updated post showing the proper syntax. 发布此答案后,页面刷新,并看到rdelmar在他的更新后的帖子中也显示了正确的语法,这也是正确的。

Credit and thanks to him for the help in helping me think this out. 归功于他,感谢他对我的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITableViewCellStyleSubtitle单元格的分隔线未占用整个宽度 - Separator lines for UITableViewCellStyleSubtitle cells not taking the full width 如何在 Swift 中设置 UITableViewCellStyleSubtitle 和 dequeueReusableCell? - How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift? 如何在 React Native 中使用 UITableViewCellStyleSubtitle 获得原生 UITableView? - How to get a native UITableView with UITableViewCellStyleSubtitle in React Native? 如何在FSCalendar中显示多行字幕 - How to display subtitle with multiple lines in FSCalendar 如何在多行上显示UIButton标签? - How to display a UIButton label on multiple lines? 如果使用“ dequeueReusableCellWithIdentifier:forIndexPath:”方法,如何设置UITableViewCellStyleSubtitle - How to Set UITableViewCellStyleSubtitle if use “dequeueReusableCellWithIdentifier:forIndexPath:” method 是否可以在同一行中显示UITableViewCellStyleValue1和UITableViewCellStyleSubtitle - Is it possible to display UITableViewCellStyleValue1 and UITableViewCellStyleSubtitle within the same row 如何在iOS中创建一个非常自定义的uibutton,如UITableViewCellStyleSubtitle - How to create a very custom uibutton like UITableViewCellStyleSubtitle in iOS 无法在tableView的多行中显示UILabel - Unable to display UILabel in multiple lines in tableView UITextView不能多行显示 - UITextView doesn't display in multiple lines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM