2

Question about changing the cell style. In storyboard, i've created a cell: content: dynamic prototypes

in code, number of sections: 2.

Section one can use the layout created in the storyboard. The cells are filling perfect with data from the database. I'm trying to change the layout for cells in section 2 to the value1 style.

How can this be done?

Some of the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cartCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    switch (indexPath.section) {

        case 0:
        {    
            // Configure the cell...
            NSInteger row = [indexPath row];
            [[cell textLabel] setText:[cartItems objectAtIndex:row]];

            return cell;
            break;
        }

        case 1:
        {


            NSInteger row = [indexPath row];
            switch (row) {
                case 0:
                    [[cell textLabel] setText:@"Total1"];
                    break;
                case 1:
                    [[cell textLabel] setText:@"Total2"];
            }

            return cell;
            break;
        }  
    }
}
4

2 回答 2

2

这样做的方法是在情节提要中创建 2 个不同的动态原型,每个原型都有自己的标识符。然后在 cellForRowAtIndexPath 中,只需在 switch 语句或 else-if 子句中使用您想要的标识符将单元格出列:

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

    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.textLabel.text = self.theData[indexPath.section][indexPath.row];
        return cell;

    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell_value1" forIndexPath:indexPath];
        cell.textLabel.text = self.theData[indexPath.section][indexPath.row];
        cell.detailTextLabel.text = @"detail";
        return cell;

    }
}
于 2013-07-29T14:29:23.427 回答
0

您需要为单元格设置单元格样式尝试以下代码

There are four inbuilt styles supported in iOS
    1. UITableViewCellStyleDefault
    2. UITableViewCellStyleValue1
    3. UITableViewCellStyleValue2
    4. UITableViewCellStyleSubtitle

试着和他们一起玩,得到你想要的结果

静态 NSString *reuseIdentifer = @"ReuseIdentifier";

 // Try to reuse the cell from table view
        UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];

        // If there are no reusable cells; create new one
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault


                                       reuseIdentifier:reuseIdentifer];
            }
cell.textLabel.tex = @"Your desired text";
return cell;
于 2013-07-29T14:26:14.193 回答