Can we initialize a list from a specific index in lazy column? - android-jetpack-compose

I want to initialize a list from a specific index in lazy column.
For example: I have a list of 10 items. I want the list to load from 5. index when first loaded.
The method I'm using now is to scroll to the 5th index after loading the list. But instead I want to initialize the list directly from an index I want. Is it possible?

you can use itemsIndexed() extension and filter the list
LazyColumn() {
itemsIndexed(viewModel.list) { index, item ->
if(index >5){
// your composable here
}
}
}

Related

How to Empty array in swift on logout

Hi there guys I have movies array in MoviesViewController which has a tableView and where I display tableCells. And I have logout in SidebarMenuController which onTap goes to LogInController. When I hit logout I want to empty the movies array from MoviesViewController and go to LogInController. How to go about this?
I would try creating an Event class that holds the array of movies. Then add an optional event property to MoviesViewController and observe setting it. On sign out, make the array empty, and set the event for the view controller. So the view controller would have something like:
var event: Event? {
didSet {
moviesArray = event?.movies
tableView.reloadData()
if moviesArray.isEmpty {
presentLogin()
}
}
}
This could also be used to populate the array if getting from a service.

Swift combine different type into one array to display data for UITableView

I have tableview where I need to show few sections. You should imagine this table like a playlist of your songs. In the top first section I need to display a cell with button which will add more songs to the playlist and other sections of tableview are header titles of Music category (like pop, rock and etc). Each of these sections contains cells which are songs names.
I have an array of songs called like this: var songsGroups = [SongGroup]. Which is actually my datasource.
SongGroup contains few properties:
var categoryName: String
var songs: [Songs]
But the problem appears on the next level. I every time need to check indexPath.section and do like this:
if indexPath.section == 0 {
// this is a section for ADD NEW SONG BUTTON cell no need in header title as there is no data repression only static text on the cell.
} else {
var musicCategoryName = songsGroups[indexPath.seciton - 1]. categoryName
headerTitle.title = musicCategoryName
}
As you see my code became magical by adding this cool -1 magical number. Which I replay don't love at all.
As an idea for sure I can try to combine my ADD NEW SONG BUTTON section (by adding some additional object) with songsGroups array and create NSArray for this purposes. Like in Objective-C as you remember. So then my datasource array will looks like this:
some NSArray = ["empty data for first cell", songsGroups[0], songsGroups[1]... etc]
So then there is no need to check any sections we can trust our array to build everything and even if I will add more empty data cells there is no need for me to handle my code via if block and adding tons of magical numbers.
But the issue I see here that we don't use explicit types of array and it's upset.
So maybe you know more beautiful solutions how to resolve my issue.
You can introduce a helper enum:
enum Section {
case empty
case songCategory(categoryName: String, songs: [String])
}
Your data source would then look something like this:
let datasource: [Section] = [.empty, .songCategory(categoryName: "Category1", songs: ["Song 1", "Song2"])]
So now you can use pattern matching to fill the table view:
let section = datasource[indexPath.section]
if case let .songCategory(categoryName, songs) = section {
headerTitle.title = categoryName
} else {
// this is a section for ADD NEW SONG BUTTON cell no need in header title as there is no data repression only static text on the cell.
}
I am not sure if I understand you right. But is seems to me that you want to display
1) something that lets the user add a new song by tapping a button, and
2) a table of songs, sectioned into groups.
If this is the case, why don’t you put the add new song button in the table header view, and all your song groups and songs in a 2-dim array used as your dataSource?

Maintain selected cells after sorting a table view in Swift

I have a table view with multiple selection enabled. Users are supposed to be able to select multiple options in a list and then change the sort order of the list. I would like to be able to keep the same cells selected, but the order of them will have changed from the sort. I'm not seeing a way to achieve this so any suggestions are appreciated.
If you have a "selected" indicator in your data structure, you could use it to rebuild the tableView selection.
If you can't add such an indicator to the data, you could perform your sort in two steps: 1) compute the new element indexes, 2) apply these indexes to your data array to perform the sort.
With the sort indexes, you can rebuild the selection based on the new location of the original indexes:
For example:
// Assuming your tableview is named "tableView",
// and the data array is named "dataArray",
// and you sort uses a member of dataArray called "sortKey"
// prepare list of new indexes for sorted data
let newIndexes = dataArray.enumerated().sorted{ $0.1.sortKey < $1.1.sortKey }.map{$0.0}
// determine new indexes of current selection
let newSelection = tableView.indexPathsForSelectedRows?
.map{IndexPath(row:newIndexes.index(of:$0.row)!, section:0)}
// sort and reload the data
dataArray = newIndexes.map{ self.dataArray[$0] }
tableView.reloadData()
// reapply the selection
tableView.indexPathsForSelectedRows?.forEach{tableView.deselectRow(at:$0, animated:false)}
for indexPath in newSelection ?? []
{ self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none) }

Specify indexPath.row for a new item in an array

I have an iOS project I'm working on using Xcode7 and Swift. The user can add items to an Array called "details" that is viewed in a TableView. Is there a way to specify the location of where you want the new item to be? Let's say I have 5 items in my array and I want the next added item to be placed in location indexPath.row for position 3. How do I do that? How can I make the new details[indexPath.row] be 3 for that item?
You should add the item into the "details" array at a specific index, then reload the table view.
details.insert("This String", atIndex: 3)
self.tableView.reloadData()
Let's say details is:
var details = ["Hello", "Hi", "Bye", "Goodbye", "No"]
Wherever you want to add the next item (I'm assuming position 3 means the 3rd index):
details.insert("Hey", atIndex: 3)
Then reload the table view:
tableView.reloadData()
Just insert the item in details at the requested index and call tableView.reloadData()or tableView.insertRowsAtIndexPaths:withRowAnimation: passing the same index wrapped in [NSIndexPath]. But first check if the index is not out of bounds.

could not be able to work with list control in flex 4.5

I want to be selected that new Item in list when I give new value to dataProvider but it selects the item that was firstly selected, and how to make that selected permanently on App start up.
You if want to select the list item after added to dataProvider. Probably you follow this
list.selectedIndex = list.dataProvider.length -1;
Sometimes if you added at desired position like
list.dataProvider.setItemAt(obj,2);
list.selectedIndex = 2
If you want to selected item permanently on App start up.(Make sure that your array collections bind-able to list)
public function onCreationComplete(event:FlexEvent):void
{
callLater(function():void
{
list.selectedIndex = list.dataProvider.length -1;
list.ensureIndexIsVisible(list.selectedIndex); // Viewport
});
}

Resources