简体   繁体   English

在表格视图中水平滚动 UITableView Controller

[英]Scroll UITableView horizontal in Table View Controller

I have a Table View Controller Scene which has a Table View Controller which has a Table View which has a Table View Cell which has the Content View which has my 3 UILabels.我有一个表视图 Controller 场景,它有一个表视图 Controller,它有一个表视图,它有一个表视图单元格,它有一个内容视图,它有我的 3 个 UILabel。 All it well, it displays my data and I can scroll up and down through my data just fine.一切都很好,它显示了我的数据,我可以很好地上下滚动浏览我的数据。

However, my data in my Table View Cell is wider than the screen.但是,我的表格视图单元格中的数据比屏幕宽。 I would like to be able to scroll the entire table horizontally so all the data in my table is viewable.我希望能够水平滚动整个表格,以便可以查看表格中的所有数据。 I was able to add a UIScrollView to the Content View and then each individual cell was capable of scrolling horizontally, but the data is columnar so scrolling it as a table makes more sense.我能够将 UIScrollView 添加到内容视图中,然后每个单独的单元格都能够水平滚动,但数据是柱状的,因此将其作为表格滚动更有意义。 I understand that UITableView is incapable of scrolling horizontal for whatever reason, in which case it seems reasonable to put the entire table into a UIScrollView.我知道 UITableView 无论出于何种原因都无法水平滚动,在这种情况下,将整个表格放入 UIScrollView 似乎是合理的。 However there seems to be no possible way to insert the UIScrollView between the Table View and the Table View Controller in IB.但是,似乎没有办法在 IB 中的表视图和表视图 Controller 之间插入 UIScrollView。

Any suggestions as to how to go about implementing this seemingly basic functionality work?关于如何实现这个看似基本的功能工作的 go 有什么建议吗? I have to be missing something really basic.我必须错过一些非常基本的东西。

I don't think it's easy to achieve what you're mentioning using just a table view.我认为仅使用表格视图来实现您提到的内容并不容易。 One way would be each cell of the table view to be a collection view with horizontal scrolling.一种方法是将表格视图的每个单元格作为具有水平滚动的集合视图 Here's an article on how to do that. 是一篇关于如何做到这一点的文章。

Another option would be to use a collection view with a custom flow layout so you can provide both vertical and horizontal scrolling.另一种选择是使用带有自定义流布局的集合视图,这样您就可以同时提供垂直和水平滚动。 If I'm not wrong, what you want to achieve is something like that .如果我没记错的话,你想要实现的是这样的 It shows how to do it, but it's a bit old article and code is in Objective-C.它显示了如何做到这一点,但它有点老了,代码在 Objective-C 中。

Also, take a look at this StackOverflow question where it's suggested to do it with a scroll view and a collection view.另外,看看这个StackOverflow 问题,建议使用滚动视图和集合视图来完成。

Generally speaking, search around a bit on StackOverflow and the web.一般来说,在 StackOverflow 和 web 上搜索一下。 I found many sites/articles discussing about this topic, so you'll definitely come up with a solution.我发现很多网站/文章都在讨论这个话题,所以你肯定会想出一个解决方案。

So it appears that there is no simple way to make a UITableViewController scroll horizontally.因此,似乎没有简单的方法可以使 UITableViewController 水平滚动。 By scroll horizontal I mean maintain vertical scrolling AND add the ability to scroll horizontal to view UITableViewCell's whose data elements are wider than the screen and to scroll the entire table as a coherent table.水平滚动是指保持垂直滚动并添加水平滚动以查看数据元素比屏幕宽的 UITableViewCell 的功能,并将整个表格滚动为连贯的表格。 I don't want to turn the UITableView 90 deg so it will scroll horizontal at the expense of scrolling vertical and I don't want to scroll the data in each cell individually.我不想将 UITableView 旋转 90 度,因此它将以垂直滚动为代价进行水平滚动,并且我不想单独滚动每个单元格中的数据。 The solution I came across for UITableView that was most like what I'm talking about is here but I didn't implement this solution.我遇到的 UITableView 的解决方案与我所说的最相似,但我没有实施解决方案。 Instead I changed my entire approach and deleted a bunch of code.相反,我改变了我的整个方法并删除了一堆代码。 Because I was displaying spreadsheet like data I created a CSV file and opened it using the Quick Look QLPreviewController.因为我正在显示类似数据的电子表格,所以我创建了一个 CSV 文件并使用 Quick Look QLPreviewController 打开它。 It opens the file and provides both vertical and horizontal scrolling and allows the user to save or share or email the file which was something else I needed to implement so this solution killed the proverbial 2 birds with 1 stone and it was pretty simple.它打开文件并提供垂直和水平滚动,并允许用户保存或共享或 email 文件,这是我需要实现的其他东西,所以这个解决方案用 1 块石头杀死了众所周知的 2 只鸟,它非常简单。

In my view controller I imported QuickLook.h and adopted the protocol.在我看来 controller 我导入了 QuickLook.h 并采用了该协议。

#import <UIKit/UIKit.h>
#import <QuickLook/QuickLook.h>

@interface MyTableViewController : UITableViewController <QLPreviewControllerDataSource>

@end

I declare 2 properties in my view controller我在我的视图中声明了 2 个属性 controller

@interface TimersTableViewController ()
@property NSString *csvFile;
@property QLPreviewController *qlController;
@end

The protocol implementation in my controller is:我的 controller 中的协议实现是:

// quick look preview controller protocol methods.  there is only one file and it is the csv file
// this controller will allow the use to decide what they want to do with the file. DML 11/21/2019
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
     return [NSURL fileURLWithPath:self.csvFile];
}

Then I wrote my data to a file and called QLPreviewController to display it.然后我将我的数据写入一个文件并调用 QLPreviewController 来显示它。

- (void)setLogs:(NSArray<LogRecords *> *)logs{
    _logs = logs;
    // allocate the quick look preview controller to display the csv file.  
    if (self.qlController == nil){
        self.qlController = [[QLPreviewController alloc] init];
        // set self as it's datasource
        self.qlController.dataSource = self;
    }
    // get the path to the NSDocumentDirectory which
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    // the path I want is the first object in the array
    NSString *documentsDirectory = [paths objectAtIndex:0];
    // set the cvs file name
    self.csvFile = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"MyCSVFile.csv"];
    // use NSFileManaget to create the file because it needs to exist before it can be opened with NSFileHandle 
    NSFileManager *fm = [NSFileManager defaultManager];
    [fm createFileAtPath:self.csvFile contents:nil attributes:nil];
    NSFileHandle  *file = [NSFileHandle fileHandleForWritingAtPath:self.csvFile];
    // write the header to the csv file
    [file writeData:[@"date,time,message" dataUsingEncoding:NSUTF8StringEncoding]];
    // for my app I loop through the logs and write each of them to the file
    for (int i = 0; i < self.logs.count; i++){
        [file writeData:[[[self.logs objectAtIndex:i] toCsvString] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [file closeFile];
    // make sure quick look preview controller gets latest csvFile 
    [self.qlController reloadData];
    // display the quick look preview controller
    [self presentViewController:self.qlController animated:YES completion:nil];
}

This solved both of my issues at once: scrolling horizontally and allowing the user access to the file to save or share it.这一次解决了我的两个问题:水平滚动并允许用户访问文件以保存或共享它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM