How can I get the selected row index in a grid?
Something like
int selectedRowIndex = grid.getSelectedRowIndex();
I already have the following if it can help
Set<Record> selectedRow = (Set<Record>)((SelectionModel) grid.getSelectionModel()).getSelectedItems();
Thank you
Related
I have a UICollection View and am creating a custom animation when a user taps on a cell.
There are three columns in the collection view. Is there a quick way to figure out, given a index path, the column that was tapped?
If you have a single section, you can just use modulo arithmetic on the item of the index path.
let column = indexPath.item % 3 // gives a value from 0 to 2
Yes. Assuming you setup the columns as sections in the collection view, you should just be able to check the section of the indexpath.
An IndexPath is primarily made of two parts, a row, and a section. If you have 3 sections than indexPath.section == 0 is the first column, indexPath.section == 1 is the second, and indexPath.section == 2 is the third
Are there any way to get all cells on the row from UICollectionView given the indexPath.row?
I guess you can make something like this:
var itemsInRow1: [Int] = [1,2,3]
var cellsInRow: [Cells] = []
for item in 0..<itemsInRow1 {
cellsInRow.append(collectionView.cellForItem(at: IndexPath.init(row: item,
section: 0)))
}
But i guess it depends on how you have your dataSourceModel setup
There is no such method. You have to mathematically calculate according to the collection view width and some calculation according to the row number you enter.
I have two questions :
1- Is it possible to know if a cell indexPath.row has changed in a UITableView ? for example if I have an element at index (0) and then I add another one, the first element will be at index (1). Is it possible to detect this change ?
2- Is it possible to fnd the index of an element by the cell title ? for example I have X,Y,Z element in the tableview and I want to know in which cell 'X' is placed ?
thanks guys,
Generally speaking, it's common practice to isolate model and view. What I mean by this, is separate the storage of the data and the view that's rendering it.
For example, you may be creating a table view of color names. Therefore, you could store an array of color names as a property of your view controller
class UIColorViewController: UITableViewController{
let colors = ["Black", "Blue", "Yellow"]
As I'm sure you're doing, you can assign the view controller as the data source of the table view so you can tell it how many sections you want, the number of rows you want in a section, and what to render onto a cell.
In this elementary example, the number of sections could be 1 and the number of rows in this section would be the length of the colors array. Now when rendering data onto the cell, you simply index the colors array by the index path.
fun tableView(UITableView, cellForRowAt: IndexPath){
var color = colors[indexPath.row]
cell = //dequeue cell
cell.textLabel?.text = color
return cell
}
So by separating the data from the view, if you want to find what cell a particular value is at, you can simply search through the array where the color is at and the index returned would be equivalent to what cell this color is or will be at depending on if that cell has been rendered onto the screen yet.
For example using the colors array, if I were looking for "Blue"
for (index, color) in self.colors.enumerated{
if color == 'Blue'{
print("Found Blue at index \(index)")
}
}
When you want to add a cell or delete a cell, you simply modify the colors array which is storing the model data, perform the appropriate table view related action, and the invariant remains that the index of the color in the array would be equivalent to the index of the cell in the table view.
I'm working on a UITableView where I need to change the background color of every second cell to help visibility of the data.
To do this, I tried making a simple variable called "cellAlteration" which I set to 0 before Loading the data (So the first cell should always be white)
var cellAlteration: Int = 0
Then where I setup my cell I do the following:
// #BackgroundColor
if cellAlteration == 0 {
self.backgroundColor = UIColor.whiteColor()
cellAlteration = 1
} else {
self.backgroundColor = UIColor.formulaWhiteColor()
cellAlteration = 0
}
(I know this should be changed to a BOOL datatype instead of Int, but that seems irrelevant for my issue at this moment)
Here is an image of the result:
As you can see it's not alternating correctly.
When I've been trying to test this, it looks like it gets the cells that is loaded first correctly (UITableView only loads and displays the cells you can see) - the problem with the coloring doesn't seem to happen until I scroll down and the UITableView has to load and display new cells.
So since my simple variable check approach isn't working, what's the correct way to do cell alternation in Swift?
Any help would be greatly appreciated :)!
You don't need a variable to do this. Base the color on whether the row is even or odd. Put this in cellForRowAtIndexPath after you dequeue a cell,
if indexPath.row % 2 == 0 {
cell.backgroundColor = UIColor.whiteColor()
}else{
cell.backgroundColor = UIColor.formulaWhiteColor()
}
I have a Vaadin Table with some Filters and sorts. Is it possible to get the ID of the row over and under the current selected row?
Object currentRow = table.getValue(); //current selected row
Object nextRow = table.nextItemId(currentRow);
Object previousRow = table.prevItemId(currentRow);