spacenation / swiftui-grid

:rocket: SwiftUI Grid layout with custom styles

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

setting a max number of posts for a Category

hasanals opened this issue · comments

commented

Hello,
I'm trying to set a max of 9 posts to be displayed in the Grid.
I have an array of posts that have different categories, I want each category to have a total of 9 posts. How can I do that with Grid?

This is the code I'm using now

`VStack(spacing: 15){

            Grid(0..<self.userDefaultData.forSalePosts.count){ i in
                
                    if self.userDefaultData.forSalePosts[i].Category == "\(self.FirebaseRefCategory)"{
                        
                        
                                        GridCell(title: self.userDefaultData.forSalePosts[i].Title  , image: self.userDefaultData.forSalePosts[i].image)
                                        
                                        
                                        
                                    }
                
                 
            }
        .gridStyle(StaggeredGridStyle(.vertical, tracks: 3, spacing: 10))

    }`

Thanks,
Hasan

Grid(self.userDefaultData.forSalePosts.suffix(9)){ index in
    
    if index.Category == "\(self.FirebaseRefCategory)"{
        
        
        GridCell(title: index.Title  , image: index.image)
        
        
        
    }
    
    
}
.gridStyle(StaggeredGridStyle(.vertical, tracks: 3, spacing: 10))

}
commented
Grid(self.userDefaultData.forSalePosts.suffix(9)){ index in
    
    if index.Category == "\(self.FirebaseRefCategory)"{
        
        
        GridCell(title: index.Title  , image: index.image)
        
        
        
    }
    
    
}
.gridStyle(StaggeredGridStyle(.vertical, tracks: 3, spacing: 10))

}

Thanks for your fast reply, I tried the above it gave me an error

Cannot convert value of type 'Array<forSaleM>.SubSequence' (aka 'ArraySlice<forSaleM>') to expected argument type 'Range<Int>'

Can you try the following one?:

Grid(self.userDefaultData.forSalePosts.[..<9]){ index in
    
    if index.Category == "\(self.FirebaseRefCategory)"{
        
        
        GridCell(title: index.Title  , image: index.image)
    
    }   
}
.gridStyle(StaggeredGridStyle(.vertical, tracks: 3, spacing: 10))

}
commented

Can you try the following one?:

Grid(self.userDefaultData.forSalePosts.[..<9]){ index in
    
    if index.Category == "\(self.FirebaseRefCategory)"{
        
        
        GridCell(title: index.Title  , image: index.image)
    
    }   
}
.gridStyle(StaggeredGridStyle(.vertical, tracks: 3, spacing: 10))

}

Also doesn't work, giving me an error
Thread 1: Fatal error: Array index is out of range