Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.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 如何在UITableViewCellStyleSubtitle中显示多行_Ios_Tableview_Cell - Fatal编程技术网

Ios 如何在UITableViewCellStyleSubtitle中显示多行

Ios 如何在UITableViewCellStyleSubtitle中显示多行,ios,tableview,cell,Ios,Tableview,Cell,所以我的应用程序中有一个下载管理器。今天我自己承担了使它活跃起来的责任 我已经实现了UITableViewCellStyleSubtitle,并且正在正确显示 我想添加多行到它。现在我只能选择文件大小或格式化日期 我怎么能两者兼得呢?i、 e Cell Title Date: (followed by file size) or File Size: 下面是我正在使用的相关代码 - (UITableViewCell *)tableView:(UITableView *)tableVie

所以我的应用程序中有一个下载管理器。今天我自己承担了使它活跃起来的责任

我已经实现了UITableViewCellStyleSubtitle,并且正在正确显示

我想添加多行到它。现在我只能选择文件大小或格式化日期

我怎么能两者兼得呢?i、 e

Cell Title
Date: (followed by file size) or
File Size:
下面是我正在使用的相关代码

    - (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;
    }

感谢您的高级帮助。

我尝试过这个方法,出于某种原因,将numberOfLines设置为2对我也不起作用,但将其设置为大于2的任何值,或将其设置为0都有效

您需要正确设置两个字符串的格式。这是不正确的语法

[NSString stringWithFormat:dateString, @"%@", [self formattedFileSize:fileSize]]
应该是这样,

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

我明白了。只是需要调整字符串的格式

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@\n%@", dateString, [self formattedFileSize:fileSize]];
在发布此答案时,页面刷新,并看到rdelmar在其更新的帖子中也显示了正确的语法


感谢他帮助我解决了这个问题。

是的,我也尝试过使用cell.detailtextlab.numberOfLines=2,但没有成功。我想我需要弄清楚如何正确设置。cell.detailTextLabel.text=[NSString stringWithFormat:dateString,@“%@,[self-formattedFileSize:fileSize]]。有了这个日期字符串,它会显示日期,但是文件大小代码被省略了。@ChrisOSX,你是说你试过3吗?请尝试0,看看是否有效。@ChrisOSX,您在哪里尝试将这两个字符串添加到详细信息标签?我只看到你在设置文件大小。您需要将两个字符串连接为一个字符串,中间有一个“\n”,以使字符串的第二部分转到第二行。[NSString stringWithFormat:dateString,@“%@,[self-formattedFileSize:fileSize];就是它被添加的地方。也许我用错了,我不知道。我见过它以常规文本格式使用,但不适用于提取实际文件数据的文本。@ChrisOSX您应该将该代码(formattedFileSize:方法)添加到您的问题中,因为这可能就是问题所在。