【问题标题】:Programiticaly Multiple Columns in Tableview iosTableview ios中的程序化多列
【发布时间】:2014-12-04 11:14:26
【问题描述】:

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

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

【问题讨论】:

  • 您的问题在UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, i, 40.0f) 行。你所有的标签都在同一个地方...... 0.0, 0.0。是主要问题
  • 我应该如何纠正这个问题?
  • 您想将一个单元格分成 3 列...对吗?
  • 是的,有点像那样,然后在单击单元格之后我想要那个标签文本。

标签: ios objective-c cocoa-touch uitableview


【解决方案1】:

这个怎么样?

// 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;

}

【讨论】:

  • 如果我想在所有单元格中添加标签怎么办?
【解决方案2】:

感谢 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;

}

【讨论】:

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