简体   繁体   English

UITableView更改单元格样式

[英]UITableView Change the cell style

I have 2 table views I want to customise the cell style of the second view depending on the selected row in the first table view. 我有2个表视图我想根据第一个表视图中选定的行自定义第二个视图的单元格样式。 it works when in customising the accessories but it does't for the style. 它在定制配件时起作用但不适合风格。 am using iOS 7.1 and Xcode 5.1 as i using the following code 我使用iOS 7.1和Xcode 5.1作为我使用以下代码

thanks in advance 提前致谢

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
    }

    cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    cell.textLabel.text = [array objectAtIndex:indexPath.row];

    if ([selectedRow isEqualToString:@"Cars"]) {
        [cell setAccessoryType: UITableViewCellAccessoryDetailButton];

    }
    if ([selectedRow isEqualToString:@"Houses"]) {
       NSArray *temp = [[dic objectForKey:selectedRow]valueForKey:@"Job"];

        cell.selectionStyle = UITableViewCellStyleSubtitle;
        cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
        cell.detailTextLabel.text = [temp objectAtIndex:indexPath.row];             
    }
    if ([selectedRow isEqualToString:@"Libraries"]) {
        cell.selectionStyle = UITableViewCellStyleValue1;
    }

    return cell;
}

You can't change a cell's style without creating a new cell. 如果不创建新单元格,则无法更改单元格的样式。

The line: 这条线:

cell.selectionStyle = UITableViewCellStyleValue1;

makes no sense. 没有意义。 You are trying to change the cell's selection style by passing in a enum value for a cell's style. 您试图通过传入单元格样式的枚举值来更改单元格的选择样式。 That's two different concepts. 这是两个不同的概念。

When you want to update a cell's style (not the selection style), you need to call UITableView initWithStyle:reuseIdentifier: again passing in the desired style. 当您想要更新单元格的样式(而不是选择样式)时,需要调用UITableView initWithStyle:reuseIdentifier:再次传递所需的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM