如果响应类型不总是相同,如何在表视图单元格上显示动态API数据

问题描述 投票:0回答:1

我有一个API请求,其中包含问题和相关响应。如果问题是例如1.,则响应为按钮如果问题为2,则响应为Switch如果问题是3.,则。 rewponse是图片。根据响应,我必须添加按钮,开关或图像。

API数据是动态的。

我不知道该如何进行!我试过的是我添加了一个表格视图单元格然后根据响应,我要向该单元格添加一个子视图!问题是,问题很多。滚动视图后,子视图将被重用!

cell. textLable = Question[indexpath.row]
let responseType = Responses[indexPath.row]


if responseType == “button”{
cell.addSubview(buttonCell)
}

if responseType == “image”{
cell.addSubview(imageCell)
}

if responseType == “switch”{
cell.addSubview(swichCell)
}

[当我滚动tableView时,我的subView被添加到anyWhere,因为单元格被重用了!

ios swift xcode uitableview
1个回答
0
投票

尝试将if语句更改为以下内容:

if responseType == “button”{
    cell.addSubview(buttonCell)
} else if responseType == “image”{
    cell.addSubview(imageCell)
} else {
    cell.addSubview(swichCell)
}
© www.soinside.com 2019 - 2024. All rights reserved.