Создайте таблицу на коде Objective-C без ViewController - PullRequest
0 голосов
/ 16 октября 2019

Я не могу создать таблицу для задачи-c, потому что я начинаю изучать цель-c.

Google не может помочь мне, я пытался, надеюсь, что вы можете помочь мне

Ответы [ 2 ]

0 голосов
/ 18 октября 2019

ЕСЛИ ВЫ ИСПОЛЬЗУЕТЕ XIB

Вам необходимо создать файл tablviewcell и создать собственную ячейку.

Импортировать файл ячейки тоже.

import "selectedcell.h"

  • (void) viewDidLoad

{[super viewDidLoad];

array =[[NSArray alloc]initWithObjects:@"AAAA",@"BBBB",,@"CCCC", nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds 
style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.tablview registerNib:[UINib nibWithNibName:@"SelectedTableViewCell" 
bundle:nil] forCellReuseIdentifier:@"cellidentifier"];
self.tablview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
[self.view addSubview:tableView];

}

  • (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section {return array.count;}

  • (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {

    SelectedTableViewCell * cell = [tableView dequeueReusableCellWidenIIIDforIndexPath: indexPath];

    cell.selectionStyle = UITableViewCellSelectionStyleGray;cell.label.text = [Nsstring stringwithformate: @ "% @", [array objectatindex: indexpath.row]];

    return cell;}

  • (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {// После выбора строки}

  • (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {return 50;}
0 голосов
/ 16 октября 2019

Для ячейки табличного представления по умолчанию с массивом:

- (void)viewDidLoad
{
    [super viewDidLoad];

    arrData=[[NSArray alloc]initWithObjects:@"ONE",@"TWO",,@"THREE", nil];
    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;

    [self.view addSubview:tableView];
}

#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section
{
    return arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [arrData objectAtIndex:indexPath.row];
    return cell;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...