Как перенести Object c в Swift aout collection - PullRequest
0 голосов
/ 01 августа 2020

Я взял указанный ниже веб-сайт в качестве ссылки и попытался преобразовать его в код Swift. Обратитесь к разделу «collectioViewLayout», мне сложно привести все в порядок.

Я признателен. UICollectionVIew circleLayout

Исходный код выглядит следующим образом: -

- (CGSize)sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if ([[self.collectionView.delegate class] conformsToProtocol:@protocol(UICollectionViewDelegateFlowLayout)]) {
    return [(id)self.collectionView.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
}
return CGSizeMake(0, 0);}

Мой исправленный быстрый код выглядит следующим образом: -

func sizeForItem(at indexPath: IndexPath?) -> CGSize {
    if type(of: collectionView.delegate) is UICollectionViewDelegateFlowLayout {
        if let indexPath = indexPath {
            return collectionView!.delegate?.collectionView!(collectionView!, layout: self, sizeForItemAt: indexPath) ?? CGSize.zero
        }
        return CGSize.zero
    }
    return CGSize(width: 0, height: 0)
}

Однако некоторые ошибки возникли. (см. ниже)

Часть 1) Приведение из 'UICollectionViewDelegate? .Type' к несвязанному типу 'UICollectionViewDelegateFlowLayout' всегда терпит неудачу

Часть 2) Неверные метки аргументов в вызове (есть ': layout: sizeForItemAt:', ожидалось ': targetIndexPathForMoveFromItemAt: toProposedIndexPath:')

Есть ли кто-нибудь, кто может помочь мне исправить эту проблему быстрый код, пожалуйста?

1 Ответ

0 голосов
/ 01 августа 2020

Код Objective- C проверяет тип collectionView.delegate, но в Swift вам нужно выполнить приведение, чтобы сообщить компилятору, что вы уверены, что это действительно тип UICollectionViewDelegateFlowLayout {

func sizeForItem(at indexPath: IndexPath?) -> CGSize {
    // "as?" does the cast, evaluating the expression to nil if it fails.
    if let delegate = collectionView!.delegate as? UICollectionViewDelegateFlowLayout,
        // you can combine these two checks into one if statement. Just separate them with ","
        let indexPath = indexPath {
        return delegate.collectionView?(collectionView, layout: self, sizeForItemAt: indexPath) ?? .zero
    }
    return .zero
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...