Swift - Reload the last 20 rows in A UITableView - swift

I'm working on a news page in an app and am attempting to reload the last 20 rows in a TableView controller whenever a fetch request is made to our API. Results are loaded in batches of 20, and I'd prefer not to reload the whole TableView whenever 20 more results are added to the table, as this causes flickering on fast scrolling.
I know I should use self.tableView.reloadRowsAtIndexPaths(), but I'm not sure as to how I obtain the index paths of last 20 rows in the table.

Use a for loop...
if there are 30 rows, this method will reload the last 20 rows (row 10 - 30). Remember, the index paths start at row 0 not 1.
for (var i = 9; i < 29; i++) {
self.tableView.reloadRowsAtIndexPaths(i)
}

Related

Clear Contents of Specific Ranges

I am still new to VBA. I wanted to clear all the contents of the data (Row 3 to Row 12, Row 15 to Row 24, etc) below the yellow headers, without deleting all of the headers as shown in the photos (Fig 1 becomes Fig. 2). The headers go all the way down to row 109 (increments of 12 from Row 1, so Rows 1,13,25 ...85). I have a code but its too basic and long:
Sub Clear_All()
Set Unitsheet = ThisWorkbook.Worksheets("Sheet"1)
Unitsheet.Range("A3:F12").ClearContents
Unitsheet.Range("A15:F24").ClearContents
.
.
.
.'up to
Unitsheet.Range("A111:F120").ClearContents
End Sub
I need a code that is short, since the rows may reach up to more than 1000.
Any help will be much appreciated.
|
|
V
Sub clear()
Dim i, rows As Long
rows = ActiveSheet.UsedRange.rows.Count
For i = 1 To rows
If Sheet1.Cells(i, 1).Interior.ColorIndex = -4142 Then
Sheet1.Cells(i, 1).EntireRow.ClearContents
End If
Next
End Sub
this function finds all used rows in sheet1
it iterates all rows , if color of cell in A column has no color index (-4142) it clears all contents in entire row

Office.js Word Add-In: Performance Issue with Updating Values in Large Tables

Summary:
Updating values in large Word tables (larger than 10 by 10) is very slow.
Performance gets exponentially worse with table size.
I'm using myTable.values = arrNewValues. I've also tried
myTable.addRows("end", rows, arrNewValues). Where arrNewValues is a
2D array.
I've also tried using updating via getOoxml() and
insertOoxml(), but ran into other issues I haven't been able to
resolve, but has good performance.
Slow performance seems to be caused by "ScreenUpdating" (same issue exists in VBA and is solved via ScreenUpdating=false). I believe it is critically important to add the ability to temporarily turn off ScreenUpdating.
Is there another way to improve table updating performance?
Background:
My add-in (https://analysisplace.com/Solutions/Document-Automation) performs document automation (updates content in a variety of Word docs). Many customers want to be able to update text in largish tables. Some documents have dozens of tables (appendices). I have run into the issue where updating these documents is unacceptably slow (well over a minute) due to the table updates.
Update time by table size:
2 rows by 10 columns: .33 seconds
4 rows by 10 columns: .52 seconds
8 rows by 10 columns: 1.5 seconds
16 rows by 10 columns: 5.5 seconds
32 rows by 10 columns: 20.8 seconds
64 rows by 10 columns: 88 seconds
Sample Office.js Code (Script Lab):
function updateTableCells() {
Word.run(function (context) {
var arrValues = context.document.body.tables.getFirst().load("values");
return context.sync().then(
function () {
var rows = arrValues.values.length;
var cols = arrValues.values[0].length;
console.log(getTimeElapsed() + "rows " + rows + "cols " + cols);
var arrNewValues = [];
for (var row = 0; row < rows; row++) {
arrNewValues[row] = [];
for (var col = 0; col < cols; col++) {
arrNewValues[row][col] = 'r' + row + ':c' + col;
}
}
console.log(getTimeElapsed() + 'Before setValues ') ;
context.document.body.tables.getFirst().values = arrNewValues;
return context.sync().then(
function () {
console.log(getTimeElapsed() + "Done");
});
});
})
.catch(OfficeHelpers.Utilities.log);
}
Sample Word VBA Code:
VBA performance is similar to the Office.js performance without ScreenUpdating = False. With ScreenUpdating = False, performance is instant.
Sub PopulateTable()
Application.ScreenUpdating = False
Dim nrRow As Long, nrCol As Long
Dim tbl As Word.Table
Set tbl = ThisDocument.Tables(1)
For nrRow = 1 To 32
For nrCol = 1 To 10
tbl.Cell(nrRow, nrCol).Range.Text = "c" & nrRow & ":" & nrCol
Next nrCol
Next nrRow
End Sub
Article explaining slow performance: see "Improving Performance When Automating Tables": https://msdn.microsoft.com/en-us/library/aa537149(v=office.11).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-3
Posts indicating there is no "ScreenUpdating = False" in Office.js: ScreenUpdating Office-js taskpane and Equivalent to Application.ScreenUpdating Property in office-js Excel add-in
Sounds like we won't see it any time soon.
Post related to the updating tables via getOoxml() and insertOoxml(): Word Office.js: issues with updating tables in ContentControls using getOoxml() and insertOoxml()
This is probably not the answer you're looking for, but I have been working with a word add in for validation of software, and we are talking about updating 500-1000 rows with lots of little formatting changes.
Anyway one thing I found that helped is to scroll somewhere else in the document before you make the changes to the table. Just the act of looking at it will slow it down 10-20x. It's not always instant but near.

How to quickly add many columns to GWT DataGrid

I am currently trying to create a DataGrid that can take an entity with a list of values as a row. Each value in the list is in its own column in the DataGrid. The entities' lists of values may have different sizes, so the DataGrid will have a variable number of columns. I have noticed that when I try to create the DataGrid and loop over the process of adding each of the column to the DataGrid, the time it takes to add the columns does not grow linearly.
Here is the code I was using to test the quickness of adding the columns
DataGrid<String> table = new DataGrid<String>();
table.setPageSize(25);
int NUM_COLUMNS = 40;
for (int i = 0; i < NUM_COLUMNS; i++) {
GWT.log("Adding column "+i);
TextColumn<String> nameColumn = new TextColumn<String>() {
public String getValue(String object) {
return object;
}
};
table.addColumn(nameColumn, "Column " + i);
table.setColumnWidth(nameColumn, 100, Unit.PX);
}
ArrayList<String> data = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
data.add("row "+i);
}
table.setRowCount(data.size(), true);
table.setRowData(0, data);
table.setWidth("100");
This took about 48 seconds, give or take 1 second, every time I ran it. It seems that loading less than 10 columns were fairly quickly, but as the number of columns grew, the time it took to load it grew exponentially.
Is there another way to add columns to the DataGrid that would be quicker? Thanks in advance.
One question you might want to ask yourself is if there's a better way to do it. A table with 40 columns (IMO) seems inefficient. In general, you're going to have significant performance loss when loading more than ~15 columns in a DataGrid, and FlexTable isn't any better.
I've worked with DataGrid quite a bit and haven't seen any of the behavior you're talking about, though in my case they typically only have 10 or fewer columns with several thousand rows. (Data is of course paged and not being jammed in all at once.)
One thing I've noticed does speed it up is pre-rendering. Are you adding the table to the DOM prior to adding all these columns, or are you adding them all first? Lots of time can be spent waiting for the DOM to update. If you're adding it to the page after rendering everything, you're probably looking at the best speed you'll get, since there's no built-in function for adding multiple columns simultaneously.

iPhone SDK: parsing html table and displaying in iphone tableview

I am using the hpple plugin to parse and display html elements in my iphone app.
The problem i'm having is say there is a table on the webpage I am trying to parse, with several rows, (that may change from time to time). How do I go about getting the number of rows in this table, and iterating through each row and getting the different text content on each row. Is this possible with the hpple plugin?
Thanks in advance.
If all that want to do is count the rows in a table with class "tableClass" then you might try something like this
// Count the number of rows in a particular table
NSString *searchString = [NSString stringWithFormat:#"//table[#class='tableClass']/tr;
NSArray *tableRows = [xpathParser search:searchString];
NSInteger rows = [tableRows count];
NSLog(#"There are %d table Rows", rows);
// For loop to step through the rows
for(int j = 1; j <= rows; j++) {
searchString = [NSString stringWithFormat:#"//table[#class='tableClass']/tr[%d]/td", j];
NSArray *tableCells = [xpathParser search:searchString];
}
This should step through the table row by row and each time scrape the individual data cells (I didn't test it so there are no guarantees.) The problem with this is that it will be very slow if your table has more than a few rows.
You are better off just calling the table and scraping all the td cells at once from the table and then deciding how they make up the rows. This way is easy if the number of cells in a row stays constant.

iPhone UITableViewController move only specific rows in a table

I have implemented a standard UITableViewController. I have made only some rows in the table to be moveable, ie. canMoveRowAtIndexPath returns true only for some indexPaths (for rows in the first part of the table). I would like to allow exchange of only those rows that are moveable, ie. for which canMoveRowAtIndexPath returns true.
Eg. my table looks like this:
Row 1
Row 2
Row 3
Row 4
Row 5
Rows 1, 2, 3 are moveable. So I want to implement the following behavior: row 1 can be exchanged only with rows 2 or 3. Similary, row 2 can be exchanged only with rows 1 and 3.
This is one possible layout:
Row 3
Row 1
Row 2
Row 4
Row 5
However, I don't want this to happen:
Row 3
Row 5
Row 2
Row 4
Row 1
How to achieve this?
Keep in mind that it's actually only moving one row, not exchanging.
What you want is to implement tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath
See How to limit UITableView row reordering to a section
You can control the movement within section and between section.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
// Do not allow any movement between section
if ( sourceIndexPath.section != proposedDestinationIndexPath.section)
return sourceIndexPath;
// You can even control the movement of specific row within a section. e.g last row in a Section
// Check if we have selected the last row in section
if ( sourceIndexPath.row < sourceIndexPath.length) {
return proposedDestinationIndexPath;
} else {
return sourceIndexPath;
}
// you can use this approach or logic to check the index of the rows in section and return either sourceIndexPath or targetIndexPath
}