getting the same value in each cell of table view in objective-c - iphone

i have check by printing the value of the cell.textlable.text in each iteration of the loop each time the value of it is as i want, but as the time of output on the simulator the value is come same for each row of cell.
my code:
for(i=0;i<[appDelegate.books count];i++)
{
Book *aBook= [appDelegate.books
objectAtIndex:i];
NSString *string1 =aBook.Latitude;
NSString *trimmedString = [string1
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];
double myDouble = [trimmedString doubleValue];
NSString *string2=aBook.Longitude;
NSString *trimmedString1 = [string2
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];
double mDouble = [trimmedString1 doubleValue];
if((((myDouble<(a+5.021777))&&(myDouble>(a-
8)))||((mDouble<=(b+10))&&(mDouble>=(b-10)))))
{
NSString *a1=aBook.AreaName;
NSString *b1=[a1 stringByAppendingString:#","];
NSString *s=aBook.STREET_NAME;
NSString *c=[b1 stringByAppendingString:s];
cell.textLabel.text = c ;
}
}
return cell; }

The problem is likely this line:
for(i=0;i<[appDelegate.books count];i++)
That's going to loop through every item in appDelegate.books. Assuming that that's an NSArray and that this code is inside your -tableView:cellForRowAtIndexPath: method, replace that line and the next with the following:
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
(This, of course, assumes that the row numbers match the index numbers in the array. If not, adjust to match.)

Related

Convert String into special - splitting an NSString

I have a string like: "mocktail, wine, beer"
How can I convert this into: "mocktail", "wine", "beer"?
the following gives you the desired result:
NSString *_inputString = #"\"mocktail, wine, beer\"";
NSLog(#"input string : %#", _inputString);
NSLog(#"output string : %#", [_inputString stringByReplacingOccurrencesOfString:#", " withString:#"\", \""]);
the result is:
input string : "mocktail, wine, beer"
output string : "mocktail", "wine", "beer"
You need to use:
NSArray * components = [myString componentsSeparatedByString: #", "];
NSString *string = #"mocktail, wine, beer";
//remove whitespaces
NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//get array of string
NSArray *array = [trimmedString componentsSeparatedByString:#","];
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (NSString *trimmedString in array) {
NSString *newString = [NSMutableString stringWithFormat:#"'%#'", trimmedString];
[newArray addObject:newString];
}
//merge new strings
NSString *finalString = [NSString stringWithFormat:#"%#", [newArray objectAtIndex:0]];
for (NSInteger i = 1; i < [newArray count]; i++) {
finalString = [NSString stringWithFormat:#"%#, %#", finalString, [newArray objectAtIndex:i]];
}
Without knowing spesifically about iOS or objective-c, I assume you could use a split function.
In almost any higher level programming language there is such a function.
Try:
Objective-C split
This gets you an array of Strings. You can then practically do with those what you want to do, e.g. surrounding them with single quotes and appending them back together. :D

Array in UITextView

I have a text view in which I want to display NSArray *result as text of the text view.
For eg:
result={#"home",#"Office",#"Park",#"Market",nil};
textView text should be:
home
office
park
market
for(int i =0 ; i <[Array Count] ;i++)
{
self.textView.text = [NSString stringWithFormat:#"%# %#",self.textView.text, Array objectAtIndex:i];
}
Correct any spelling mistake.
You can use componentsJoinedByString: method, like this:
NSString *text = [result componentsJoinedByString:#" "];
You can use a different separator instead of #" ".
You can use a for loop as such:
NSString *str =#"";
for (NSString *tmp in result) {
str = [NSString stringWithFormat:#"%# %#",str,tmp];
}
If you want each entry on a new line replace the space with "\n".

Problem with isEqualToString: method and NSInteger

This is my code:
for (int i=0; i<countingArray.count; i++) {
NSDictionary *element=[countingArray objectAtIndex:i];
NSString *source=[element objectForKey:#"id"];
NSInteger count= [[element objectForKey:#"count"] integerValue];
NSLog("source: %#",source); //Works
NSLog("count %d",count); //Don't Work! Error at this line
for(int c=0; c<subscriptions.count; c++) {
SubscriptionArray * element =[subscriptions objectAtIndex:c];
NSLog(#"sorgente sub %#",element.source);
NSLog(#"sorgente counting %#",source);
if([source isEqualToString:element.source]) {
element.count=count;
[subscriptions replaceObjectAtIndex:c withObject:element];
NSLog(#"equal");
//this part of code is never been executed but I'm sure that
//the if condition in some cases returns true
}
}
}
When I try to NSLog("count %d",count); my app crash without any information about.
I've also another problem with if([source isEqualToString:element.source]) I'm sure that some times the condition return true... How can I remove blank space? Like the trim function in php? thanks
Change:
NSString *source=[element objectForKey:#"id"];
NSInteger count= [[element objectForKey:#"count"] integerValue];
to:
NSString *source=[element valueForKey:#"id"];
NSInteger count= [[element valueForKey:#"count"] integerValue];
For removing blank spaces you can try:
NSString *trimmedString = [yourString stringByReplacingOccurrencesOfString:#" " withString:#""];
I didn't notice the first two NSLog statements. They're both of the form NSLog("something: %#", something). That is a C string literal, whereas NSLog takes an NSString for its format. This will lead to a crash. You want NSLog(#"source: %#", source).

Capture first line of NSString

How do I capture the first line from a NSString object?
I currently am assigning the entire NSString object to the title of my textView, but only want to assign the first line of the string. My current code like this this:
self.textView.text = [[managedObject valueForKey:#"taskText"] description];
You want
self.textView.text = [[[[managedObject valueForKey: #"taskText"] description] componentsSeparatedByString: #"\n"] objectAtIndex:0];
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
If you’re targeting iOS 4.0 and later, you can use -[NSString enumerateLinesUsingBlock:]:
__block NSString *firstLine = nil;
NSString *wholeText = [[managedObject valueForKey:#"taskText"] description];
[wholeText enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
firstLine = [[line retain] autorelease];
*stop = YES;
}];
self.textView.text = firstLine;
An alternative approach which is probably the most efficient and straightforward:
NSString* str = [[managedObject valueForKey:#"taskText"] description];
self.textView.text = [str substringWithRange:[str lineRangeForRange:NSMakeRange(0, 0)]];

iPhone SDK: How to manipulate a txtField

Trying to manipulate the contents of a txtField in the textFieldDidEndEditing. What we want to do is rotate the entire contents left and then append the current character at the end.
This is to allow a user to enter a money value without a decimal point.
This is what we have so far but I'm not sure this is the best way to approach this. Nor is this code working as expected.
Any help appreciated.
//easier input for total
if (textField == txtGrandTotal)
{
//copy contents of total to string2
NSString *string1 = txtGrandTotal.text;
NSMutableString *string2;
//now string2 contains the buffer to manipulate
string2 = [NSMutableString stringWithString: string1];
//so, copy it to strMoney2
strMoney2 = string2;
int charIndex;
//for all character in strMoney2, move them rotated left to strMoney1
for (charIndex = 0; charIndex < [strMoney2 length]; charIndex++)
{
NSString *strChar = [strMoney2 substringWithRange: NSMakeRange(charIndex, 1)];
[strMoney1 insertString:strChar atIndex:charIndex+1];
}
//now append the current character to strMoney1
NSString *strCurrentChar = [string1 substringWithRange: NSMakeRange([string1 length], 1)];
[strMoney1 appendString:strCurrentChar];
//move manipulated string back to txtGrandTotal
txtGrandTotal.text = strMoney1;
}
Out of a zillion ways to approach this, this is quite short:
NSString *res = [NSString stringWithFormat:#"%#%#",
[input substringFromIndex:1],
[input substringToIndex:1]
];
or shorter:
NSString *res = [[input substringFromIndex:1]
stringByAppendingString:[input substringToIndex:1]];