Segue'ing to detail UItableView while using Sections - ios

Can someone please help me. Im very new at this and trying to seque to my detail screen, which works fine as long as i don't use sections. The files i try to grab in the detail are html files.
(numbered 1 - 53). When is seque to detail without sections i simply use
htmlFile = [artikelen objectAtIndex:[path row]] to seque;
When i use sections [path row] will be something like [0.1] instead of continues numbering.
I sure hope someone can help me solve and understand this problem.
Thanks.
Jaco
[super viewDidLoad];
artikelen = [[NSMutableArray alloc]initWithObjects:#"artikel1",#"artikel2",#"artikel3",#"artikel4",#"artikel5",#"artikel6",#"artikel7",#"artikel8",#"artikel9",#"artikel10",#"artikel11",#"artikel12",#"artikel13",#"artikel14",#"artikel15",#"artikel16",#"artikel17",#"artikel18",#"artikel19",#"artikel20",#"artikel21",#"artikel22",#"artikel23",#"artikel24",#"artikel25",#"artikel26",#"artikel27",#"artikel28",#"artikel29",#"artikel30",#"artikel31",#"artikel32",#"artikel33",#"artikel34",#"artikel35",#"artikel36",#"artikel37",#"artikel38",#"artikel39",#"artikel40",#"artikel41",#"artikel42",#"artikel43",#"artikel44",#"artikel45",#"artikel46",#"artikel47",#"artikel48",#"artikel49",#"artikel50",#"artikel51",#"artikel52",#"artikel53",#"artikel54", nil];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 14;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSArray *sectionTitles = [[NSArray alloc]
initWithObjects:#"Hoofdstuk I.", #"Hoofdstuk
II.",#"Hoofdstuk III.",#"Hoofdstuk IV.",#"Hoofdstuk IVA.",#"Hoofdstuk IVB.",#"Hoofdstuk
V.",#"Hoofdstuk VA.",#"Hoofdstuk VI.",#"Hoofdstuk VII.",#"Hoofdstuk VII B.",
#"Hoofdstuk VIIC.",#"Hoofdstuk VIII.",nil];
return sectionTitles;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
if ( section == 0 ) return #"Hoofdstuk I. Algemeen";
if ( section == 1 ) return #"Hoofdstuk II. De instelling";
if ( section == 2 ) return #"Hoofdstuk III. Samenstelling ";
if ( section == 3 ) return #"Hoofdstuk IV. Het overleg ";
if ( section == 4 ) return #"Hoofdstuk IVA. bevoegdheden ";
if ( section == 5 ) return #"Hoofdstuk IVB. Hgegevens aan de ondernemingsraad";
if ( section == 6 ) return #"Hoofdstuk IVC. Verdere bevoegdheden van de";
if ( section == 7 ) return #"Hoofdstuk V. De centrale ";
if ( section == 8 ) return #"Hoofdstuk VA.";
if ( section == 9 ) return #"Hoofdstuk VI. De algemene";
if ( section == 10 ) return #"Hoofdstuk VII.";
if ( section == 11 ) return #"Hoofdstuk VII. A.de overheid";
if ( section == 12 ) return #"Hoofdstuk VII. B ";
if ( section == 13 ) return #"Hoofdstuk VIII";
return #"Other";
}
To display the right number of rows i used:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ( section == 0 ) return 1;
if ( section == 1 ) return 4;
... etcetra
return 0;
}
To display the right cell i've used:
if ( indexPath.section == 12 ) mijnRij += 45; //7a
if ( indexPath.section == 13 ) mijnRij += 56; //8
cell.textLabel.text = [artikelen objectAtIndex:mijnRij];
return cell;

If you use a better data structure it will make the coding much easier.
I suggest that your main structure should be an array of dictionaries. Each dictionary should match a section and contain two keys. The first key should give you the section header and the second key should give you an array that matches the rows in that section.
That way you don't need so many conditions. The section number tells you which dictionary to use and the row tells you where to look in the dictionary's sub-array.

Related

Is there any arithmetic formula that can test all given numbers are in row, like [ 3 5 4 ]

I m making a card game where 3 random numbers are generated..I need to check are these numbers Row numbers...
like 4 6 5 and 23,24,22. are row numbers
I have made method but I think there should be easy arithmetic formulas
I have tried this and working well, but I need simple arithmatic formula to avoid use of array and for
bool isAllInRow(int num1, int num2,int num3)
{
//subject : tinpati
List<int> numbers=[num1,num2,num3];
bool is_in_row=true;
numbers.sort();
if(numbers[0]==1 && numbers[1]==12 && numbers[2]==13)
return true;
for(int x=0;x<numbers.length-1;x++)
{
if(numbers[x]-numbers[x+1]!=-1)
{
is_in_row=false;
break;
}
}
return is_in_row;
}
So you want to know if the cards form a straight, with aces both low and high.
Is the "three cards" fixed, or would you want to generalize to more cards?
Sorting should be cheap for such a short list, so that's definitely a good start. Then you just need to check the resulting sequence is increasing adjacent values.
I'd do it as:
bool isStraight(List<int> cards) {
var n = cards.length;
if (n < 2) return true;
cards.sort();
var first = cards.first;
if (first == 1 && cards[1] != 2) {
// Pretend Ace is Jack if n == 3.
// Accepts if remaining cards form a straight up to the King.
first = 14 - n;
}
for (var i = 1; i < n; i++) {
if (cards[i] != first + i) return false;
}
return true;
}
This code rejects card sets that have duplicates, or do not form a straight.
I think you are looking for Arithmetic Progression.
bool checkForAP(List<int> numberArr) {
numberArr.sort();
int diff = numberArr[1] - numberArr[0];
if (numberArr[2] - numberArr[1] != diff) {
return false;
}
return true;
}
And modify your function like
bool isAllInRow(int num1, int num2,int num3) {
//subject : tinpati
List<int> numbers=[num1,num2,num3];
bool is_in_row=true;
numbers.sort();
if(numbers[0]==1 && numbers[1]==12 && numbers[2]==13)
return true;
return checkForAP(numbers);
}
Note: remove sort in AP method as it is of no use. Since your numbers
list length is 3 I directly compared numbers for AP, the same can also
be written for n numbers with for.
bool checkForAp(numberArr) {
numberArr.sort();
int diff = numberArr[1] - numberArr[0];
for(int i = 2; i< numberArr.length ;i++) {
if (numberArr[i] - numberArr[i - 1] != diff) {
return false;
}
}
return true;
}
You could do it like this:
bool isAllInRow(int num1, int num2,int num3) {
if (num1 == num2 || num2 == num3) return false;
var maxNum = max(num1, max(num2, num3));
var minNum = min(num1, min(num2, num3));
return (maxNum - minNum == 2) || (minNum == 1 && maxNum == 13 && num1 + num2 + num3 == 26);
}

How can I color a row using jsPDF AutoTable based on a single cell value?

I'm trying to create a PDF based off of an interactive/dynamic table using jsPDF + AutoTable. The last cell in a row will either say "Yes" or "No" - if the cell says "Yes" the entire row should be highlighted.
The closest I've come is getting the cell to highlight, but I need the entire row to highlight. I seem to be having trouble tying the cell to the row index.
This is my code so far:
willDrawCell: function (data) {
if (data.cell.text == "Yes") {
doc.setFillColor(67, 160, 71);
}
}
Thanks for any help.
I was able to resolve this!
Before my doc.autoTable() call, I calculate the index where the "Yes" is, and store those values in an array. I then loop over each row index and setFillColor as appropriate. Here is what my code looks like now:
willDrawCell: function (data) {
if (data.cell.text == "Yes") {
doc.setFillColor(67, 160, 71);
}
for (let i = 0; i < ready_rows.length; i++) {
if (data.row.index === ready_rows[i]) {
doc.setFillColor(239, 154, 154);
}
}
}
here currentRow is row which given in auto table
var res1 = doc.autoTableHtmlToJson(document.getElementById(m));
var idmm=document.getElementById(n);
var clength=res1.columns.length;
var crow=res1.rows.length;
doc.autoTable(res1.columns, res1.data, {margin: {top: vy+25},pageBreak: 'auto',styles: {cellPadding: 1.5,fontSize:fsize , },fontStyle: 'bold',drawRow: function (row, data) {
currentRow1 = row;
currentRow1.height =30;
if((currentRow1.cells[0].text[0].includes('Total')) || (currentRow1.cells[0].text[0].includes('Avg'))||(currentRow1.cells[0].text[0].includes('count'))||(currentRow1.cells[0].text[0].includes('Min'))||(currentRow1.cells[0].text[0].includes('Max')))
{
1
for(var i=0;i<res1.columns.length;i++)
{
currentRow1.cells[i].styles.fontStyle = "bold";
currentRow1.cells[i].styles.font = "sans-serif" ;
currentRow1.cells[i].styles.fillColor = [243,205,204];
currentRow1.cells[1].styles.columnWidth="wrap";
currentRow1.cells[1].styles.FontSize=30;
}
}
},columnStyles: {0: {columnWidth: columnwidthrgroup},},});
```

Using Ternary operation in the if condition - Objective C

I am trying to check to find out whether or not numberOfItemsPerSection is bigger than 3 in the if condition. It always returns true. Then I decided to debug.
How is it possible that indexPath.row equals to 1 and numberOfItemsPerSection = 20 and it goes into to the if condition.
What am I doing wrong by using the following ternary operator?
if(indexPath.row == (numberOfItemsPerSection > 3) ? (numberOfItemsPerSection-4) : numberOfItemsPerSection)
{
}
Use parenthesises to resolve priority. Change the condition by below way. Just cover your turnery condition with parenthesis. It will resolve the turnery operator first and then it will compare it to indexPath.row.
if(indexPath.row == ((numberOfItemsPerSection > 3) ? (numberOfItemsPerSection-4) : numberOfItemsPerSection))
NSInteger desiredRow = numberOfItemsPerSection > 3 ? (numberOfItemsPerSection-4) : numberOfItemsPerSection;
if(indexPath.row == desiredRow) { ... // do your coding }
You can write:
if (indexPath.row == (numberOfItemsPerSection > 3 ? numberOfItemsPerSection - 4 : numberOfItemsPerSection)) { ... }
Or if you don't want to hurt your eyes:
BOOL desiredRow = numberOfItemsPerSection > 3 ? numberOfItemsPerSection - 4 : numberOfItemsPerSection;
if (indexPath.row == desiredRow) { ... }

How to compare two column in a spreadsheet

I have 30 columns and 1000 rows, I would like to compare column1 with another column. IF the value dont match then I would like to colour it red. Below is a small dataset in my spreadsheet:
A B C D E F ...
1 name sName email
2
3
.
n
Because I have a large dataset and I want to storing my columns in a array, the first row is heading. This is what I have done, however when testing I get empty result, can someone correct me what I am doing wrong?
var index = [];
var sheet = SpreadsheetApp.getActiveSheet();
function col(){
var data = sheet.getDataRange().getValues();
for (var i = 1; i <= data.length; i++) {
te = index[i] = data[1];
Logger.log(columnIndex[i])
if (data[3] != data[7]){
// column_id.setFontColor('red'); <--- I can set the background like this
}
}
}
From the code you can see I am scanning whole spreadsheet data[1] get the heading and in if loop (data[3] != data[7]) compare two columns. I do have to work on my colour variable but that can be done once I get the data that I need.
Try to check this tutorial if it can help you with your problem. This tutorial use a Google AppsScript to compare the two columns. If differences are found, the script should point these out. If no differences are found at all, the script should put out the text "[id]". Just customize this code for your own function.
Here is the code used to achieve this kind of comparison
function stringComparison(s1, s2) {
// lets test both variables are the same object type if not throw an error
if (Object.prototype.toString.call(s1) !== Object.prototype.toString.call(s2)){
throw("Both values need to be an array of cells or individual cells")
}
// if we are looking at two arrays of cells make sure the sizes match and only one column wide
if( Object.prototype.toString.call(s1) === '[object Array]' ) {
if (s1.length != s2.length || s1[0].length > 1 || s2[0].length > 1){
throw("Arrays of cells need to be same size and 1 column wide");
}
// since we are working with an array intialise the return
var out = [];
for (r in s1){ // loop over the rows and find differences using diff sub function
out.push([diff(s1[r][0], s2[r][0])]);
}
return out; // return response
} else { // we are working with two cells so return diff
return diff(s1, s2)
}
}
function diff (s1, s2){
var out = "[ ";
var notid = false;
// loop to match each character
for (var n = 0; n < s1.length; n++){
if (s1.charAt(n) == s2.charAt(n)){
out += "–";
} else {
out += s2.charAt(n);
notid = true;
}
out += " ";
}
out += " ]"
return (notid) ? out : "[ id. ]"; // if notid(entical) return output or [id.]
}
For more information, just check the tutorial link above and this SO question on how to compare two Spreadsheets.

Search a row for matches

I'm very new to Google Sheets and particularly using the Apps Script API to make functions for Sheets.
My goal is to search 3 cells in a row, and if two of three contain an 'X', the third which does not turns blue.
Currently the sheet has conditional formatting as follows:
X = Green
Empty = Red
? = Orange
! = Blue
So the intent is to change the Empty cell to ! if the other two cells in the row are X.
Image for reference:
Essentially, I need a function which can check a range of cells for their contents, but I don't know how to properly use the API, if someone could give me a bit of help it would be greatly appreciated.
Note: This is NOT for a project of any sort, this is just for my friends and myself.
Edit
My thoughts were to have the range as the function parameter (A1:C1 for instance) and then access the cell's data for the range and check them against each other. My issue is that I don't know how to use the API to get this data.
If the scenario mentioned before applies, you can use this method:
function checkRow() {
var ss = SpreadsheetApp.getActive();
var activeCell = ss.getActiveCell();
var currentRow = activeCell.getRow();
var currentCol = activeCell.getColumn();
var allRange = ss.getRange("A1:C3");
var activeValue = activeCell.getValue();
var secondCell, thirdCell;
if(activeValue == "x")
{
if ( currentCol == 1 )
{
secondCell = allRange.getCell( currentRow, currentCol+1 );
thirdCell = allRange.getCell( currentRow, currentCol+2 );
} else if ( currentCol == 2 )
{
secondCell = allRange.getCell( currentRow, currentCol-1 );
thirdCell = allRange.getCell( currentRow, currentCol+1 );
}
else if ( currentCol == 3 )
{
secondCell = allRange.getCell( currentRow, currentCol-1 );
thirdCell = allRange.getCell( currentRow, currentCol-2 );
}
if ( secondCell.getValue() == "x" && thirdCell.getValue() == "" )
{
thirdCell.setValue("!");
thirdCell.setBackground("#00FFFF");
} else if (thirdCell.getValue() == "x" && secondCell.getValue() == "" )
{
secondCell.setValue("!");
secondCell.setBackground("#00FFFF");
}
}
}

Resources