簡體   English   中英

適用於iOS的UITableView

[英]UITableView for iOS

我是Objective-C的新手。 我只是搜索並嘗試了解UITableView

我已經創建了這個UITableView 而不是所有行中的"a" ,我希望它以"abc 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

設置表視圖在IB中進行委托,或者如果您以編程方式創建,則在viewDidLoad中以動態或靜態方式填充一個數組。在NumberOfRowsInsection方法中提供數組計數,然后在適當的位置重新加載表視圖

查看本教程,我相信它將對您有幫助

UItableView指南

對於滾動,您不需要自己做任何事情,因為單元格的數量超過了提供的大小,它會自動變為可滾動狀態,並確保在UITableView屬性檢查器中啟用了滾動,請檢查圖像

在此處輸入圖片說明

快樂編碼

問候

在您的cellForRowAtIndexPath中:添加以下行

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

使用下面的代碼.....全局定義字母,然后在代碼中使用...

- (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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM