简体   繁体   English

如何制作UITableView Scroll?

[英]How do I make my UITableView Scroll?

In the following code, I push a new UITableView when I click on a button. 在下面的代码中,当我单击一个按钮时,我会推送一个新的UITableView。 The TableView has 25 rows. TableView有25行。 I can see the first 9 row and the 10th row got cut off. 我可以看到第9行和第10行被切断了。 I can't scroll down to view the rest of the rows. 我无法向下滚动以查看其余行。 How do I make the TableView scrollable? 如何使TableView可滚动? Thank you. 谢谢。

- (IBAction)showListPicker:(id)sender {
    ListPicker *lp = [[ListPicker alloc] initWithStyle:UITableViewStyleGrouped];
    [self setTitle:@"Home"];
    [[self navigationController] pushViewController:lp animated:YES];
    [lp release];
}

The following is the declaration for ListPicker 以下是ListPicker的声明

#import <Foundation/Foundation.h>

@interface ListPicker : UITableViewController
{}
@end

The following is the implementation for ListPIcker 以下是ListPIcker的实现

#import "ListPicker.h"
#import "GameStore.h"
#import "List.h"
#import "HomeViewController.h"

@implementation ListPicker

- (id)init
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

-(id)initWithStyle:(UITableViewStyle)style{
    return [self init];
}

-(void)dealloc{
    [super dealloc];
}

-(void)viewDidLoad{
    [self setTitle:@"Select List"];
    [[self navigationController] setNavigationBarHidden:NO];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[[GameStore defaultStore] allLists] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:@"UITableViewCell"] autorelease];
    }

    NSArray *allList = [[GameStore defaultStore] allLists];
    List *mylist = [allList objectAtIndex:[indexPath row]];
    NSString *listTitle = [mylist label];

    [[cell textLabel] setText:listTitle];
    [cell setAccessoryType:UITableViewCellAccessoryNone];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    List *list = [[[GameStore defaultStore] allLists] objectAtIndex:[indexPath row]];
    [[GameStore defaultStore] setSelectedList:list];

    HomeViewController *hv = [[HomeViewController alloc] init];
    [[self navigationController] popViewControllerAnimated:YES];
}


@end

Try to add: 尝试添加:

yourTable.scrollEnabled = YES;

Also, when you create your table programmatically, make sure it is "long enough", ie, 此外,当您以编程方式创建表时,请确保它“足够长”,即,

CGRect cgRct = CGRectMake(0, 0, 320, 600);          
yourTable = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStyleGrouped];    

而不是YES ,写入true,因为它只接受bool值。

yourTable.scrollEnabled = true;

Two common ways to change the boolean state of scrolling in a UITableView: 在UITableView中更改滚动的布尔状态的两种常用方法:

1) It can be enabled either in the attributes inspector of the UITableView via Storyboard. 1)可以通过Storyboard在UITableView的属性检查器中启用它。

在此输入图像描述

2) Or programmatically inside your viewDidLoad() via ViewController. 2)或通过ViewController以编程方式在viewDidLoad()中。

import UIKit  

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

        @IBOutlet var yourTable: UITableView!

        override func viewDidLoad() {
            super.viewDidLoad()

            // assign delegate and datasource 
            yourTable.delegate = self
            yourTable.dataSource = self

            //enable UITableView scrolling in Swift 3 and Swift 4.
            yourTable.isScrollEnabled = true
    } 
}

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

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