0

我对 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
4

4 回答 4

3

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

于 2013-02-04T13:37:18.823 回答
0

检查本教程,我相信它会帮助你

UItableView 指南

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

在此处输入图像描述

快乐编码

问候

于 2013-01-17T07:23:09.337 回答
0

在你的 cellForRowAtIndexPath 中:添加这些行

char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:@"%c" , ch];
于 2013-01-17T07:25:04.017 回答
0

使用下面的代码......在全球范围内定义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;
}
于 2013-01-17T07:48:42.487 回答