display data from sqlite to label using FMDB

2019-07-26 17:10发布

问题:

How to display data from SQLite to label using FMDB like this:

I would like to display whatever the output is in this format for the user to see. No Interaction. Only display for the select query.

回答1:

If you want to add UILabel objects, just iterate through your result set, adding labels to your view. Something like:

NSInteger rowNum = 0;
NSArray *columnWidths = @[@(50), @(120), @(75), @(75)];
CGFloat rowHeight = 24.0;

FMResultSet *rs = [db executeQuery:sql];

while ([rs next])
{
    CGFloat x = 0.0;
    NSInteger colNum = 0;
    for (NSNumber *columnWidth in columnWidths)
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x,
                                                                    rowHeight * rowNum, 
                                                                    [columnWidth floatValue], 
                                                                    rowHeight)];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.borderColor = [UIColor blackColor].CGColor;
        label.layer.borderWidth = 1.0;
        label.text = rs[colNum++];
        [self.view addSubview:label];
        x += [columnWidth floatValue];
    }
    rowNum++;
}

[rs close];

I just typed that in, so I'm sure there are typos in there, but hopefully it's enough to give you an idea of what it could look like. If there are too many rows to fit on a screen, you would add a scrollview to your view, and then add the labels as a subview to that.

I also think the tableview is a wonderful approach. Or dynamically built a HTML string that you show in a UIWebView. Tons of options here. It depends a little upon what your ultimate goal is here, the nature of the data, what sort of user interaction you want.



回答2:

You can accomplish the above by using a UITableView and custom UITableViewCells.

And you can maintain an array which will act as a data source to your view controller.

Considering that you want to have the same layout as you have shared (which has four labels per row), you will be fetching the data from a database.

Since, the data fetched from a database may keep on increasing, UITableView is your best bet when displaying long list of data as memory management and cell creation is handled automatically.

If you plan to have only labels then things can get tricky for you. Because if the number of rows that you fetch using select query is too much then you will end up creating a lot of views which will cause memory issues.

And, if you don't want you user to interact with any of the rows, then implement the following method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Here is a link for creating custom cells:

http://www.appcoda.com/customize-table-view-cells-for-uitableview/

Here is a link for using table view:

http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/

Good Luck!!