【问题标题】:How to create multiple columns in UITableView i.e CustomCellView?如何在 UITableView 中创建多个列,即 CustomCellView?
【发布时间】:2016-02-29 08:06:21
【问题描述】:

我刚刚开始 iOS 开发。我正在尝试从 NSArray 在 UItableview 中显示多个列?所以请帮助我吗?我已经创建了一列,但我不知道如何创建另一列?

我做了以下事情。

// This is my code 

- (NSArray *)numbers
{

    NSArray *col1 = @[@"1", @"2", @"3", @"4", @"5"];
    return col1;

}
- (NSArray *)numberCodes
{

    NSArray *col2 = @[@"Roy", @"Ankit", @"Jhon", @"Kem", @"Pawan"];
    return col2;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.numbers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];



    //cell.textLabel.text = [self.numbers objectAtIndex:indexPath.row];
    cell.textLabel.text = [self.numberCodes objectAtIndex:indexPath.row];


    return cell;
}

【问题讨论】:

  • 能不能把你试过的代码拿出来,很容易解决
  • 请阅读how to ask good questions 并尝试编辑您的问题。有了高质量的问题,您将更快地获得更好的答案。谢谢!
  • 另外,UITableView 不是行和列意义上的表格;就其本身而言,它只有一个“列”。

标签: ios objective-c uitableview xcode7


【解决方案1】:

第一步

在您的 viewController.h 上创建一个全局数组,如

@property (nonatomic,retain) NSMutableArray *tableData;

在您的ViewController.m 上分配该数组的内存,例如

- (void)viewDidLoad

{

[super viewDidLoad];

tableData = [[NSMutableArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten",nil];

}

关于tableview Datasource 方法做喜欢

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [tableData count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 {

 static NSString *simpleTableIdentifier = @"XXXX";

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

 if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

 }

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

return cell;

 }

样品见this

【讨论】:

    【解决方案2】:

    1.您可以创建服装单元格,例如在表格中有多个字段/列。

    2.放置多个表视图,每列一个。使用自动布局以获得更好的外观。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 2021-09-21
      相关资源
      最近更新 更多