我正在将我的应用从Objective C转换为Swift。我在这个例外的所有领域做得很好。在我的目标c文件中,我有一个允许多个选择的UITableView。当用户选择单元格时,来自该对象的信息存储在数组中。当用户再次单击该单元格时,该对象将被删除。我试图找出它如何在Swift 3中工作,我可以添加对象,但我似乎无法弄清楚如何从数组中删除该对象。请指教。下面是我想要转换的目标C的代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RibbonsInfo *ribbonsInfo = [ribbonsArray objectAtIndex:indexPath.row];
UITableViewCell *cell = [ribbonTableView cellForRowAtIndexPath:indexPath];
if (ribbonTableView.allowsMultipleSelection == YES) {
if(cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedRibbons addObject:ribbonsInfo];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedRibbons removeObject:ribbonsInfo];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
答案 0 :(得分:0)
使用Swift,您只能使用Array
从index
中删除项目。因此,您需要在该数组中获取该对象的索引,然后调用selectedRibbons.remove(at: index)
。
例如。
var array = Array<String>()
array.append("apple")
array.append("banana")
array.append("orange")
print(array) // ["apple", "banana", "orange"]
if let index = array.index(of: "banana") {
array.remove(at: index)
}
print(array) // ["apple", "orange"]