在给定索引处从数组中删除图像

时间:2017-03-21 17:39:59

标签: ios swift macos uiscrollview

我创建并初始化了一个从图像数组中提取的滚动视图。因此,用户滑过一张照片,然后另一张照片,我需要根据当前图像的位置删除图像我创建了一个for循环,删除了数组中的图像i--但是这会崩溃,因为我可以&# 39;删除i。有没有办法可以做到这一点?谢谢:这是我的代码

class ViewController: UIViewController {
var imageArray = [UIImage]()

@IBOutlet weak var mainScrollView: UIScrollView!

  func scrollViewDidScroll(_ scrollView: UIScrollView) {
        for i in 0..<imageArray.count {
            if i >= 3 {
                imageArray.remove(at: i - 2)
            }
    }
}


override func viewDidLoad() {
super.viewDidLoad()
mainScrollView.frame = view.frame
imageArray = [#imageLiteral(resourceName: "dubai6"), #imageLiteral(resourceName: "dubai2"), #imageLiteral(resourceName: "dubai7"), #imageLiteral(resourceName: "dubai4"), #imageLiteral(resourceName: "dubai3"), #imageLiteral(resourceName: "dubai5"), #imageLiteral(resourceName: "DigitalDrawingPreview"), #imageLiteral(resourceName: "denarus"), #imageLiteral(resourceName: "dubai1")]
for i in 0..<imageArray.count  {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFit
    imageView.image = imageArray[i]
    let xPosition = self.view.frame.width * CGFloat(i)
    imageView.frame = CGRect(x: xPosition, y: 0, width: self.view.frame.width, height: self.mainScrollView.frame.height)

    mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
    mainScrollView.addSubview(imageView)
}

1 个答案:

答案 0 :(得分:1)

我建议使用UITableView或UICollectionView,而不是使用UIScrollView。管理数据模型非常容易,然后调用"ui.grid.selection" ,"ui.grid.exporter" $scope.exportCSV = function(){ var exportService=uiGridExporterService; var grid=$scope.gridApi.grid; var fileName="myfile.csv"; exportService.loadAllDataIfNeeded(grid, uiGridExporterConstants.ALL, uiGridExporterConstants.VISIBLE).then(function() { var exportColumnHeaders = exportService.getColumnHeaders(grid, uiGridExporterConstants.VISIBLE); $scope.gridApi.selection.selectAllVisibleRows(); var exportData = exportService.getData(grid, uiGridExporterConstants.SELECTED,uiGridExporterConstants.VISIBLE); var csvContent = exportService.formatAsCsv(exportColumnHeaders, exportData, grid.options.exporterCsvColumnSeparator); exportService.downloadFile(fileName, csvContent, grid.options.exporterOlderExcelCompatibility); $scope.gridApi.selection.clearSelectedRows(); }); }

您可以使用ScrollViewDelegate方法table.reloadData()跟踪滚动位置,并在滚动得足够远后强制重新加载。