iOS的UITableView

iOS的UITableView,ios,xcode4,tableview,Ios,Xcode4,Tableview,我对Objective-C非常陌生。我刚刚搜索并试图了解UITableView 我已经创建了这个UITableView。我不希望所有行中都出现“a”,而是希望它以类似“a b c d…”的顺序出现。如果我增加行数,它应该滚动。它不滚动,所以这里是我的代码 #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { CGRe

我对Objective-C非常陌生。我刚刚搜索并试图了解
UITableView

我已经创建了这个
UITableView
。我不希望所有行中都出现
“a”
,而是希望它以类似
“a b c d…”的顺序出现。
如果我增加行数,它应该滚动。它不滚动,所以这里是我的代码

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
    CGRect frame = self.view.frame;
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame  style:UITableViewStylePlain];
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tableView.backgroundColor = [UIColor cyanColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIDent"];
    cell.textLabel.text = @"a";
    return cell;
}
@end

查看本教程,我相信它会对您有所帮助

对于滚动,您不需要自己做任何事情,当单元格数量增加超过提供的大小时,它会自动变为可滚动,并确保在
UITableView
属性检查器中启用滚动,检查图像

快乐编码


关于

检查本教程,我相信它会对您有所帮助

对于滚动,您不需要自己做任何事情,当单元格数量增加超过提供的大小时,它会自动变为可滚动,并确保在
UITableView
属性检查器中启用滚动,检查图像

快乐编码


在您的单元格中添加“

”for行索引路径:添加以下行

char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:@"%c" , ch];

在cellForRowAtIndexPath中:添加以下行

char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:@"%c" , ch];

使用下面的代码…在全局中定义alphabetarr,然后在代码中使用它

- (void)viewDidLoad
{
    alphabetarr = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z", nil];

    CGRect frame = self.view.frame;
    UITableView *tableview = [[UITableView alloc] initWithFrame:frame];
    tableview.backgroundColor = [UIColor cyanColor];
    tableview.separatorColor = [UIColor blackColor];
    tableview.delegate = self;
    tableview.dataSource = self;
    [self.view addSubview:tableview];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [alphabetarr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell!=nil) {
        cell = nil;
    }
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

        cell.textLabel.text = [alphabetarr objectAtIndex:indexPath.row];


    return cell;
}

使用下面的代码…在全局中定义alphabetarr,然后在代码中使用它

- (void)viewDidLoad
{
    alphabetarr = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z", nil];

    CGRect frame = self.view.frame;
    UITableView *tableview = [[UITableView alloc] initWithFrame:frame];
    tableview.backgroundColor = [UIColor cyanColor];
    tableview.separatorColor = [UIColor blackColor];
    tableview.delegate = self;
    tableview.dataSource = self;
    [self.view addSubview:tableview];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [alphabetarr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell!=nil) {
        cell = nil;
    }
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

        cell.textLabel.text = [alphabetarr objectAtIndex:indexPath.row];


    return cell;
}


在IB中设置表视图委托,或者如果您是以编程方式创建的,则在viewDidLoad中获取一个数组,动态或静态填充。在NumberOfRowsInsection方法中提供数组计数,并在适当的位置重新加载表视图

在IB中设置表视图委托,或者如果您是以编程方式创建的,则在viewDidLoad中获取一个数组填充它动态或静态。在NumberOfRowsInsection方法中给出数组计数,并在适当的位置重新加载表视图

您需要阅读一些iOS教程。检查。也
[super viewDidLoad]
应该是该方法中的第一个调用。请查看Apple的引用@I感谢您对学习单元格for row的兴趣,索引处的单元格将按顺序逐个返回单元格,因此这里即使是第二个单元格也会返回a。尝试更改逻辑,滚动查看下面的答案,它们的框架非常漂亮。您需要阅读一些iOS教程。检查。也
[super viewDidLoad]
应该是该方法中的第一个调用。请查看Apple的引用@I感谢您对学习单元格for row的兴趣,索引处的单元格将按顺序逐个返回单元格,因此这里即使是第二个单元格也会返回a。尝试更改逻辑,滚动查看下面的答案,它们的框架非常漂亮;非常感谢。它可以用于索引,但屏幕仍然没有滚动。在viewDidLoad的末尾添加这两行,我相信您会让它正常工作[tableView SetScrolEnabled:YES];[tableView setUserInteractionEnabled:是];请移动[super viewDidLoad];显示到viewDidLoad的顶部,并告诉我它是否有效。我也这样做了,但仍然无法滚动;非常感谢。它可以用于索引,但屏幕仍然没有滚动。在viewDidLoad的末尾添加这两行,我相信您会让它正常工作[tableView SetScrolEnabled:YES];[tableView setUserInteractionEnabled:是];请移动[super viewDidLoad];显示到viewDidLoad的顶部,并告诉我它是否有效。我也这样做了,但仍然无法滚动。