是否可以从数组中删除多个项目,同时使用索引位置按照.remove(at:i)类似:
伪代码:
myArray.remove(at: 3, 5, 8, 12)
Run Code Online (Sandbox Code Playgroud)
如果是这样,这样做的语法是什么?
更新:
我正在尝试这个,它有效,但下面答案中的扩展更具可读性和合理性,并且实现了与伪代码完全相同的目标.
创建一系列"位置":[3,5,8,12]
let sorted = positions.sorted(by: { $1 < $0 })
for index in sorted
{
myArray.remove(at: index)
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的collectionView中随机添加一些横幅广告.
每个collectionView单元格都是一个基本图像(这里是方便的黑色方块),从数组中动态填充(假设它是一个非常长的数组,并称之为"longDataArray"),我将从网上获取.
我可以设法在我的collectionView中添加一些横幅广告,但问题是它违反了我的longDataArray的顺序.例如,仅在我在indexPath 6添加广告横幅时进行测试,然后广告横幅正确地显示在indexPath 6上,我能够管理单元格的宽度更改,但是在indexPath处对应的图像6我的longDataArray显然永远不会出现.
我也可以把我的longDataArray分成两部分,然后玩部分:section 0 = firstPartOfArray,section 1 = ad banner,section 2 = secondPartOfArray.但这需要花费大量精力创建不同的数组和部分,只需添加一个广告横幅,而且显然不是我想要的.
所以我的问题是,你如何在collectionView中添加横幅广告(只有一个部分),但保留indexPath逻辑?
我搜索了很多关于这一点,并且惊讶于我无法为这个问题找到任何解决方案.
你们有什么想法吗?
谢谢!
我正在加载一个数组UIcollectionview
(稍后将添加其他数据).我想随机选择集合视图项:
var indexpath = Int(arc4random_uniform(UInt32(items.count)-1)
self.collectionview.selectitem(at: **indexpath**, animated:true ,scrollPosition:UICollectionViewScrollPosition)
Run Code Online (Sandbox Code Playgroud)
这是在大胆的位置给我错误说:
无法将Int类型的值转换为预期的参数类型'IndexPath?
我可以理解这个异常,因为indexpath
swift与集合数据索引(数组索引)不同.但是我找不到任何可以将项索引转换为索引路径或任何其他方法的方法.
我是iOS的新手,所以可能不擅长为这样的问题搜索正确的关键字.对于满足此要求的任何其他方法,对您来说都是一个很大的帮助.
在swift 3之前,我习惯使用例如:
let path = self.tableView.indexPathForSelectedRow
if (path != NSNotFound) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
但是现在,因为我IndexPath
在swift3中使用了类,所以我正在寻找等价的 path != NSNotFound
支票.
Xcode8.3.1编译器错误: "二进制运算符'!='不能应用于'IndexPath'和'Int'类型的操作数"
伙计们,我对 Rxswift 完全陌生,有没有办法在 RxSwift 中实现这种情况?
我得到的是这个..但问题是我没有indexPath
datasource.sectionModels
.asObservable()
.bindTo(tableView.rx.items) { tableView, row, element in
guard let sectionType = SectionType(rawValue: indexPath.section) else { return 0 }
let indexPath = IndexPath(row: row, section: 0)
var itemForIndexPath: SectionViewModel {
return self.datasource.sectionModels.value[indexPath.section]
}
switch sectionType {
case .nickTitle, .nickIfno:
let infoCell = tableView.dequeueReusableCell(
withIdentifier: InfoTableViewCell.name,
for: indexPath
) as! InfoTableViewCell
var datasource: InfoCellDatasourceProtocol = InfoCellNormalState(text: itemForIndexPath.text)
if itemForIndexPath.errorStyle {
datasource = InfoCellErrorState(text: itemForIndexPath.text)
}
infoCell.configureCell(datasource: datasource)
}
Run Code Online (Sandbox Code Playgroud)
这就是我在 RxSwift 中需要的
func tableView(_ tableView: UITableView, …
Run Code Online (Sandbox Code Playgroud) 在对tableview数据进行某种排序后,我需要重新加载不包括标题的部分.这就是说我只想重新加载该部分中的所有行.但是在我搜索一段时间后,我找不到一个简单的方法.
reloadSections(sectionIndex, with: .none)
这里不起作用,因为它会重新加载整个部分,包括页眉,页脚和所有行.
所以我需要reloadRows(at: [IndexPath], with: UITableViewRowAnimation)
改用.但是如何获取该部分中所有行的整个indexPath.
我在一个控制器中有4个不同的collectionViews,在第一个collectionView中,我想显示3个不同的单元格。在下面的代码中,该应用程序不会崩溃,但在第一个indexPath.item为0的情况下,仅加载0:(“ cellId”)。它不会加载其他两种情况。在此先感谢您的帮助!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
switch indexPath.item {
case 0:
return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
default:
return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId4", for: indexPath)
}
} else if indexPath.item == 1 {
return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath)
} else if indexPath.item == 2 {
return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId3", for: indexPath)
} else {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
return myCell
}
}
Run Code Online (Sandbox Code Playgroud)
// 2个collectionView单元格-每个单元格中只有一个部分
// //单元格1-collectionViewController中带有reuseIdentifier“ cellId”
class TravelGuideHomeCellForStats: BaseHomeCell, …
Run Code Online (Sandbox Code Playgroud) switch-statement uicollectionview uicollectionviewcell swift indexpath
我在每行中都有一个UITableView
,UICollectionView
如下图所示。
来源:https : //ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
我的应用程序的目的是在每个表格视图行中显示一种语言的字符集。每个字符都包含在相应行内collectionview的collection view单元格内。
我遇到的问题是正在为每个tableview行显示英文字符集。
这是因为每个collectionview仅具有一个部分,因此每个collectionview使用相同indexPath.section
的零值。
我需要做的是获取collectionview所在的表视图单元格的section值,并以某种方式将其传递给
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几件事,但是找不到从其中的collectionview访问tableview部分值的方法。
我的代码有些混乱,但是通常重要的部分是
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath)
return cell
}
...
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell",
for: indexPath as IndexPath)
//format inner collectionview cells
//indexPath.section is the collectionview section index but needs …
Run Code Online (Sandbox Code Playgroud) 我正在尝试访问indexPath
函数内部的数组以更新此数组的数据,但我不知道如何将indexPath
as作为参数(尤其是调用时传递的内容)传递给函数,或者这是否是解决方案。
我包括cellForRowAt
来说明如何访问此函数indexPath
。
var cryptosArray: [Cryptos] = []
extension WalletTableViewController: UITableViewDelegate, UITableViewDataSource, CryptoCellDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let crypto = cryptosArray[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WalletTableViewCell
cell.setCrypto(crypto: crypto)
cell.delegate = self
cell.amountTextField.delegate = self
return cell
}
func cellAmountEntered(_ walletTableViewCell: WalletTableViewCell) {
if walletTableViewCell.amountTextField.text == "" {
return
}
let str = walletTableViewCell.amountTextField.text
let crypto …
Run Code Online (Sandbox Code Playgroud) 我在 a 中有项目tableview
,我想重新加载或更新tableview
数据,除了 tableview 的第一行或第一个索引路径。
let visibleIndex = self.tableView.visibleCells.compactMap {
tableView.indexPath(for: $0) }
self.tableView.reloadRows(at: visibleIndex, with: .automatic)
Run Code Online (Sandbox Code Playgroud)
但是,这会重新加载所有可见单元格,我如何重新加载除第一行之外的所有可见单元格
indexpath ×10
swift ×9
ios ×8
uitableview ×5
arrays ×2
swift3 ×2
ads ×1
nsindexpath ×1
reloaddata ×1
rx-swift ×1
uiscrollview ×1
xcode ×1