iOS NSArray count - ios

I think my question is simple but it stumped me. Basically I have a NSArray of objects for example:
NSArray *arrayT = [NSArray arrayWithObjects:#"A", #"B", #"C", #"D", nil];
I want to put these objects into a TableViewCell. To display, I did
cell.positioninarray.text = [arrayT objectAtIndex:indexPath.row];
cell.letterinarray.text = [arrayT objectAtIndex:indexPath.row];
I am expecting this result
1 A
2 B
3 C
4 D
But am getting
A A
B B
C C
D D
I've looked through the NSArray help file in Apple developer website, but i don't see one that works for me.
Please help! Thanks!

cell.positioninarray.text = [NSString stringWithFormat:#"%d", (int)(indexPath.row+1)];

Replace
cell.positioninarray.text = [arrayT objectAtIndex:indexPath.row];
with
cell.positioninarray.text = [NSString stringWithFormat#"%d", ((int)indexPath.row+1)];
Explanation
You are setting the text for both positioninarray and letterinarray to be
[arrayT objectAtIndex:indexPath.row];
So you are getting A, B, C, D in both of the labels. To get 1, 2, 3, 4 in positioninarray, use the above code.

Change your code as below, indexPath.row will give you current index for the cell, and you want position start by 1 not 0 so indexPath.row + 1 will do your work, and finally convert your int value to string using NSString stringWithFormat :
cell.positioninarray.text = [NSString stringWithFormat:#"%i", (indexPath.row + 1)];
cell.letterinarray.text = [arrayT objectAtIndex:indexPath.row];

Related

NSString in NSMutableArray isn't the same on myString

I have a problem with NSString and NSMutableArray.
When I retrieve a string from mutable array ,it has more white spaces, and I don't understand why it's happening.
I'll explain, I have an array and populate it by query (using sqlite3):
NSMutableArray *fileNameAttached = [[NSMutableArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
and it's like this:
<__NSArrayM 0x15e233980>(
<__NSArrayM 0x15e26afd0>(
Allegato N. 1
)
)
When I retrieve the string Allegato N.1 with this code:
NSString *test = [NSString stringWithFormat:#"%#", [fileNameAttached objectAtIndex:0] ];
string test is like this:
(
"Allegato N. 1"
)
Why my string isn't only:
Allegato N.1
When I put it in a label it's not correct because contains () and white spaces.
The query for DB is:
NSString *query = [NSString stringWithFormat:#"SELECT fileName FROM Attach WHERE ID = '%#';",ticketID];
and works perfectly,in fact when I populate tableview cell it's OK. But I don't understand because my string test contains 3 line with more white spaces.
Please help Me.
Thank you and sorry for my english.
In your log there are two arrays.
(
(
Allegato N. 1
)
)
There are two round brackets thats means two array.
You are fetching data like NSString *test = [NSString stringWithFormat:#"%#", [fileNameAttached objectAtIndex:0] ];
That means first object of outer array so it is another array. so you need to do something like
NSArray *temp =[fileNameAttached objectAtIndex:0];
NSString *test = [NSString stringWithFormat:#"%#", [temp objectAtIndex:0] ];
I think you are using appcoda's DBManager class to do this. If it is so then you everytime got two array when load query.
Hope this will help :)

iOS - Displaying Two Words At A Time From String Array

I'm doing an RSVP reading project app where it blinks words on the screen. You can set the word chunk size (how many words you want displayed at a time) to either 1, 2, or 3. I got it working for 1 word by having my paragraph in a string and doing:
[self.textInput componentsSeparatedByString:#" ";
This makes me an array of words that I can use to blink one word at a time. How would I be able to do this with displaying 2 words at a time? Is there a way I can use this function again to do it differently, or should I iterate over this word array and make a new one with 2 word strings?
Any help or advice would be greatly appreciated as to what the best practice would to get this done. Thanks.
just like keith said create an array
NSArray *allwordsArray = [self.textInput componentsSeparatedByString:#" "];
Now you got all the info you need. Meaning you got the array with every word in it. Now its just a matter of putting it together. (I haven't tested this code)
NSMutableArray *twoWordArray = [[NSMutableArray alloc] init];
int counter=0;
for (int i=0; i<[allwordsArray count]; i++)
{
if (counter >= [allwordsArray count]) break;
NSString *str1 = [NSString stringwithformat#"%#", [allwordsArray objectAtIndex:counter]];
counter++;
if (counter >= [allwordsArray count]) break;
NSString *str2 = [NSString stringwithformat#"%#", [allwordsArray objectAtIndex:counter]];
NSString *combinedStr = [NSString stringwithformat#"%# %#", str1,str2];
[twoWordArray addObject: combinedStr];
counter++;
}
You have broken the string into components, which is on the right track. You could then make a smaller array that only includes components until you reach the chunk size. The final step would be to rejoin the string.
NSArray *components = [self.textInput componentsSeparatedByString:#" "];
NSRange chunkRange = NSMakeRange(0, chunkSize);
NSArray *lessComponents = [components subarrayWithRange:chunkRange];
NSString *newString = [lessComponents componentsJoinedByString:#" "];

How to change the values of an array and then view

I want to subtract 273 from the received values of array to view the temperature in Celsius.
cell.textLabel.text = [NSString stringWithFormat:#"Kelvin: %#",[day objectAtIndex:indexPath.row]];
Here day is an array of values from which I want to substract value 273
you can do this in two ways
in your cellforRowAtIndex
cell.textLabel.text = [NSString stringWithFormat:#"Kelvin: %d",[[day objectAtIndex:indexPath.row] intValue] - 273];
where u get the response on that place
assume that yourstr=#"345";
NSString *cal = [NSString stringWithFormat:#"%d", [yourstr intValue]-273];
add to ur array
[day add object: cal];
finally u show the normally in your tableview
cell.textLabel.text = [NSString stringWithFormat:#"Kelvin: %#",[day objectAtIndex:indexPath.row]];
cell.textLabel.text = [NSString stringWithFormat:#"Kelvin: %d",[[day objectAtIndex:indexPath.row] intValue] - 273];
Give it a try

UITableView cell subtitles from plist data

I want to add some subtitles to my UITableView cells (where I display continents, countries, other data). Already populated my first table with continents. Now I want to add numbers of countries as subtitle in every continent cell. I added:
int numberEurope;
numberEurope = [europe count]; //this works fine
int numberAfrica;
numberAfrica = [africa count]; //this works fine
NSNumber *myNum1 = [NSNumber numberWithInt:numberEurope];
NSNumber *myNum2 = [NSNumber numberWithInt:numberAfrica];
NSArray *myArray = [NSArray arrayWithObjects: myNum1, myNum2, nil];
cell.textLabel.text = ContinentName;
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"%# countries", myArray];
return cell;
But the subtitles are the same in every cell, the full array is shown: ( 2, 2) countries instead of 2 countries in the first cell and 2 countries in the second one. What am I doing wrong? The plist I use is screenshot here: Cannot Feed UITableView with .plist
You have to select proper value based on indexPath:
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"%d countries", [[myArray objectAtIndex:[indexPath row]] intValue]];

IOS Array Testing

Here is my current code:
int i = 1;
NSString * StockOneYahooFinance = [NSString stringWithFormat:#"http://finance.yahoo.com/q/hp?s=S+Historical+Prices"];
NSString * PulledStockOne = [NSString stringWithContentsOfURL:[NSURL URLWithString:StockOneYahooFinance] encoding:1 error:nil];
for (i=1;i=30;i++){
NSString *StartPulling = [[PulledStockOne componentsSeparatedByString:#"nowrap align="] objectAtIndex:i];
NSString *StartOpen = [[StartPulling componentsSeparatedByString:#">"] objectAtIndex:3];
NSString *Open = [[StartOpen componentsSeparatedByString:#"<"] objectAtIndex:0];
NSString *StartClose = [[StartPulling componentsSeparatedByString:#">"] objectAtIndex:9];
NSString *Close = [[StartClose componentsSeparatedByString:#"<"] objectAtIndex:0];
year.text = Close;
i++;
}
But to the point I click the only button on the screen and it does exactly what I want it pulls the stocks open and close price for the day. But my current issue is I want it to pull all of these as an array so how can I do this?
First thing:
for (i=1;i=30;i++){
it should be:
for (i=1;i<=30;i++){
Second one, do not increment 'int i' value on the end of loop because 'for' loop already do this. For quick'n dirty way of debugging add:
NSLog(#"Current iteration: %i", i);
as the first function in 'for' loop to see what's happening there.

Resources