0

我想在我的 iPhone 应用程序中创建这样的视图:

在此处输入图像描述

我不知道iOS中的这个控件到底是什么,也许我可以在左侧设置一个图标和文本,在右侧设置一个小标志。

我已经实现了一个 TableView,在那里我可以设置这些东西,如下所示:

    [[cell textLabel] setText:customer.name];
    [[cell textLabel] setTextColor:[UIColor blackColor]];
    [[cell imageView] setImage:[UIImage imageNamed:@"icon.png"]];
    //[[cell detailTextLabel] setText:@"Awsome weather idag"];
    cell.accessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];  

但是我怎样才能让它像图片中的那个视图一样工作呢?

4

3 回答 3

2

为了快速输出,您可以使用一些库,如 https://github.com/escoz/QuickDialog

protip,根据我的经验,当更改出现时,这样的解决方案会让您更加纠结。

有时您只想在特定视图上更改一个特定标签,这在任何现成的解决方案中都不容易。

于 2012-10-08T05:47:49.203 回答
2

这很简单,请按照以下步骤操作,如有疑问,请查看UITableView 文档

1.创建分组表视图:

以编程方式:

CGRect tableFrame = CGRectMake(0, 0, 200, 200);

UITableView *tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewGroupedStyle];
tableView.delegate = self;
tableView.dataSource = self;

[self.view addSubview:tableView];

分配一个 UITableViewController 子类(常见情况):

MyTableViewController *controller [[MyTableViewController alloc] initWithStyle: UITableViewGroupedStyle];

[self.navigationController pushViewController:controller animated:NO];

通过界面构建​​:

  1. 展开实用程序菜单(右上角图标);
  2. 选择您的表格视图(单击它);
  3. 点击属性检查器(右上角第四个图标);
  4. 在表格视图下,单击样式下拉菜单并选择分组。

2. 实现 UITableViewDataSource 协议:

基本上将这三个功能添加到您的控制器中。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.

    return 1;
}

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

    // Return the number of rows in a given section.

    return 5;
}

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

    // Configure the cells.

    return cell;
}

3. 配置细胞:

UITableViewCell 的默认样式有一个图像视图(UIImageView)、一个标题标签(UILabel)和一个附件视图(UIView)。在您提供的图像中复制表视图所需的一切。

所以,你正在寻找这样的东西tableView:cellForRowAtIndexPath:

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

    static NSString * const cellIdentifierDefault = @"default";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierAccount];

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

    if (indexPath.section == 0) {

        cell.imageView.image = [UIImage imageName:@"bluetooth_icon"];
        cell.textLabel.text = @"Bluetooth";

        // Additional setup explained later.

    }else{

        if (indexPath.row == 0) {

            cell.imageView.image = [UIImage imageName:@"general_icon"];
            cell.textLabel.text = @"General";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }else{

            cell.imageView.image = [UIImage imageName:@"privacy_icon"];
            cell.textLabel.text = @"Privacy";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }

    }

    return cell;
}

属性 accessoryType 定义了将出现在单元格右侧的内容,可以在此处找到附件类型的列表。

在第一个单元格(蓝牙)中,您需要创建一个自定义附件视图并将其分配给单元格的附件视图属性。下面给出了一个非常简单的例子来说明如何实现这一点:

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

    static NSString * const cellIdentifierDefault = @"default";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierAccount];

    if (cell == nil) {

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

        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
        label.textAlignment = NSTextAlignmentRight;

        cell.accessoryView = label;

    }else{

        label = (UILabel *) cell.accessoryView;

    }

    cell.imageView.image = [UIImage imageName:@"bluetooth_icon"];
    cell.textLabel.text = @"Bluetooth";
    label.text = @"Off";

    return cell;
}

希望这会有所帮助,马特乌斯

于 2012-10-08T01:35:28.637 回答
1

查看 UITableView 部分。这就是将这些组分开的原因。

于 2012-10-07T23:08:26.607 回答