Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在两种不同类型的UITableViewCell之间交替使用_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 在两种不同类型的UITableViewCell之间交替使用

Ios 在两种不同类型的UITableViewCell之间交替使用,ios,objective-c,uitableview,Ios,Objective C,Uitableview,Scenario=我有一个tableView,它将由一个数据数组加载(来自internet,所以我不能“硬编码”这个)。将有两个不同的tableView单元格,我想在这两种类型之间依次切换。“单元格A”的右侧有文本,左侧有图片,“单元格B”的左侧有文本,右侧有图片。下面我对所需的结果进行了一些说明(不是为了“代码”,只是为了说明tableView中单元格之间的交替) 问题=我不确定如何为两种不同类型的单元格退出ReuseIdentifier队列 Request=有人能帮我处理这个代码吗,或者至少

Scenario=我有一个tableView,它将由一个数据数组加载(来自internet,所以我不能“硬编码”这个)。将有两个不同的tableView单元格,我想在这两种类型之间依次切换。“单元格A”的右侧有文本,左侧有图片,“单元格B”的左侧有文本,右侧有图片。下面我对所需的结果进行了一些说明(不是为了“代码”,只是为了说明tableView中单元格之间的交替)

问题=我不确定如何为两种不同类型的单元格退出ReuseIdentifier队列

Request=有人能帮我处理这个代码吗,或者至少给我指出正确的方向,或者这是可能的吗


谢谢大家

这其实很简单

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0) {
        // setup and return table cell type A
    } else {
        // setup and return table cell type B
    }
}

在故事板中,应该有一个包含两个原型单元(一个用于单元a,另一个用于单元B)的表视图。为每个单元格设置标识符(比如
cellAIdentifier
cellBIdentifier

还必须为CellA和CellB创建UITableViewCell的子类。然后在prototype单元中创建用户界面,并将IBOutlets连接到UITableViewcell的子类。假设您使用一个名为
label
的IBOutlet,另一个名为
imageView

之后,您可以使用rmaddy回答:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell;
    if (indexPath.row % 2 == 0) {
        cell = [tableview dequeueReusableCellWithIdentifier:@"cellAIdentifier"];
    } else {
        cell = [tableview dequeueReusableCellWithIdentifier:@"cellBIdentifier"];
    }
    label.text = @"your text, which should be taken from your model".
    imageView.image = [UIImage imageNamed:@"yourImageName"];
    return cell;
}

祝你好运

你在用故事板吗?是的,我在用。它与storyboardID有关吗?你能给我解释一下模(“%”)在做什么吗?这是检查值是否为偶数的标准方法。如果一个数字%2的结果为零,那么它就是一个偶数。这很有趣。谢谢你和我分享!有关%的详细信息:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell;
    if (indexPath.row % 2 == 0) {
        cell = [tableview dequeueReusableCellWithIdentifier:@"cellAIdentifier"];
    } else {
        cell = [tableview dequeueReusableCellWithIdentifier:@"cellBIdentifier"];
    }
    label.text = @"your text, which should be taken from your model".
    imageView.image = [UIImage imageNamed:@"yourImageName"];
    return cell;
}