Programiticaly Tableview ios中的多个列

时间:2014-12-04 11:14:26

标签: ios objective-c cocoa-touch uitableview

我正在尝试创建一个包含多个列的表,我正在使用单元格数组。 以下是我的代码,我每次都会得到单列。

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

    TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

   cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;     

    CGFloat x = 0.0f;
    UIView *lastCol = [[cell columnCells] lastObject];
    if (lastCol) x = lastCol.frame.origin.x + lastCol.frame.size.width;

    for (int i = 0; i < 2; i++) {

         UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, i, 40.0f)] ;
        UIView *gridCell = l;

        CGRect f = gridCell.frame;
        f.origin.x += x;
        gridCell.frame = f;

        [cell.contentView addSubview:gridCell];

        CGFloat colWidth = [self widthForColumn:i];

        x += colWidth + 1.0f;
        [[cell columnCells] addObject:gridCell];
    }
    for (int i = 0; i < 2; i++) {       
        UILabel *l = (UILabel*)[cell columnCells][i];
       l.text =self.department[indexPath.row];     
       }  

    return cell;
}

2 个答案:

答案 0 :(得分:0)

这个怎么样?

// table view delegates
- (int)numberOfSectionsInTableView:(UITableView *) tableView {
    return 1;
}

- (int) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];

    double boxWidth = self.view.frame.size.width/3;

    for (int i=0;i<=2;i++) {
        UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(boxWidth*i, 0, boxWidth, 100)];
        if (i==0) {
            mView.backgroundColor = [UIColor redColor];
        } else if (i==1) {
            mView.backgroundColor = [UIColor greenColor];
        } else if (i==2) {
            mView.backgroundColor = [UIColor blueColor];
        }

        [cell addSubview:mView];
    }


    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}

答案 1 :(得分:0)

感谢Fahim,我只修改了他的代码,得到了我想要的东西。

这是我正在寻找的确切要求。

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


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];

    double boxWidth = self.view.frame.size.width/3;

    for (int i=0;i<=2;i++) {

        UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(boxWidth*i, 0, boxWidth, 100)];

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 250, 15)];
        [label setText:self.department[indexPath.row]];

        [cell addSubview:mView];
        [mView addSubview:label];

    }


    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}