Ios 用字符串数组填充UI表视图

Ios 用字符串数组填充UI表视图,ios,uitableview,Ios,Uitableview,我在任何地方都找不到一个简单、简洁的答案,我不相信XCode会让事情变得像我在那里发现的其他教程一样困难 假设我有以下数组 NSArray*days=[NSArray阵列,其对象为:@“星期日”,“星期一”,“星期二”,“星期三”,“星期四”,“星期五”,“星期六”,无]; 我有一个UI表视图,Table\u Days,我想简单地显示数组中的项目。填充我的表的正确方法是什么?以下是我的完整解释,从一个与您非常相似的案例开始: 因此,假设days存储为通过属性self.days访问的实例变量。然

我在任何地方都找不到一个简单、简洁的答案,我不相信XCode会让事情变得像我在那里发现的其他教程一样困难

假设我有以下数组

NSArray*days=[NSArray阵列,其对象为:@“星期日”,“星期一”,“星期二”,“星期三”,“星期四”,“星期五”,“星期六”,无];


我有一个UI表视图,
Table\u Days
,我想简单地显示数组中的项目。填充我的表的正确方法是什么?

以下是我的完整解释,从一个与您非常相似的案例开始:

因此,假设
days
存储为通过属性
self.days
访问的实例变量。然后将
self
设置为表视图的
datasource
,并使用以下代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!self.days) // data not ready?
        return 0;
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
        numberOfRowsInSection:(NSInteger)section {
    return [self.days count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:@"Cell"
                                        forIndexPath:indexPath];
    cell.textLabel.text = (self.days)[indexPath.row];
    return cell;
}

以下是我的完整解释,从一个与你极为相似的案例开始:

因此,假设
days
存储为通过属性
self.days
访问的实例变量。然后将
self
设置为表视图的
datasource
,并使用以下代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!self.days) // data not ready?
        return 0;
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
        numberOfRowsInSection:(NSInteger)section {
    return [self.days count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:@"Cell"
                                        forIndexPath:indexPath];
    cell.textLabel.text = (self.days)[indexPath.row];
    return cell;
}

以下是我的完整解释,从一个与你极为相似的案例开始:

因此,假设
days
存储为通过属性
self.days
访问的实例变量。然后将
self
设置为表视图的
datasource
,并使用以下代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!self.days) // data not ready?
        return 0;
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
        numberOfRowsInSection:(NSInteger)section {
    return [self.days count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:@"Cell"
                                        forIndexPath:indexPath];
    cell.textLabel.text = (self.days)[indexPath.row];
    return cell;
}

以下是我的完整解释,从一个与你极为相似的案例开始:

因此,假设
days
存储为通过属性
self.days
访问的实例变量。然后将
self
设置为表视图的
datasource
,并使用以下代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!self.days) // data not ready?
        return 0;
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
        numberOfRowsInSection:(NSInteger)section {
    return [self.days count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:@"Cell"
                                        forIndexPath:indexPath];
    cell.textLabel.text = (self.days)[indexPath.row];
    return cell;
}

您应该使用填充表视图。返回数组的
计数
,以获取行数

如果需要检测用户何时点击单元格,可以使用以下方法

@界面视图控制器
@财产(非原子,副本)NSArray*天;
@属性(非原子,强)UITableView*tableDays;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
UITableView*tableDays;//设置此项
[tableView注册表类:[UITableViewCell类]强制重用标识符:@“单元格];
tableDays.delegate=self;
tableDays.dataSource=self;
[self.view addSubview:tableDays];
self.tableDays=tableDays;
self.days=@[@“星期日”,“星期一”,“星期二”,“星期三”,“星期四”,“星期五”,“星期六];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[self.days count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath];
cell.textlab.text=self.days[indexPath.row];
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
NSString*day=self.days[indexPath.row];
NSLog(@“天:%@”,天);
}
@结束

如果您只想显示表视图,应该考虑使用A。< /P>


请注意,更好的做法是对变量使用camel case。

您应该使用填充表视图。返回数组的
计数作为行数

如果需要检测用户何时点击单元格,可以使用以下方法

@界面视图控制器
@财产(非原子,副本)NSArray*天;
@属性(非原子,强)UITableView*tableDays;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
UITableView*tableDays;//设置此项
[tableView注册表类:[UITableViewCell类]强制重用标识符:@“单元格];
tableDays.delegate=self;
tableDays.dataSource=self;
[self.view addSubview:tableDays];
self.tableDays=tableDays;
self.days=@[@“星期日”,“星期一”,“星期二”,“星期三”,“星期四”,“星期五”,“星期六];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[self.days count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath];
cell.textlab.text=self.days[indexPath.row];
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
NSString*day=self.days[indexPath.row];
NSLog(@“天:%@”,天);
}
@结束

如果您只想显示表视图,应该考虑使用A。< /P>


请注意,更好的做法是对变量使用camel case。

您应该使用填充表视图。返回数组的
计数作为行数

如果需要检测用户何时点击单元格,可以使用以下方法

@界面视图控制器
@财产(非原子,副本)NSArray*天;
@属性(非原子,强)UITableView*tableDays;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
UITableView*tableDays;//设置此项
[tableView注册表类:[UITableViewCell类]强制重用标识符:@“单元格];
tableDays.delegate=self;
tableDays.dataSource=self;
[self.view addSubview:tableDays];
self.tableDays=tableDays;
self.days=@[@“星期日”,“星期一”,“星期二”,“星期三”,“星期四”,“星期五”,“星期六];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[self.days count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath];
cell.textlab.text=self.days[indexPath.row];
返回单元;