Change color of text in UITableViewCell based on the text - iphone

I'm trying to change the text of a custom UITableViewCell based on whether the text says Closed or not. However, when the text is Closed, the color will not change. Here is a code snippet. http://pastie.org/1078690
Thanks.

Simple equality does not work for NSString objects. Instead of
if ([dict objectForKey:#"status"] == #"Closed") {
do
if ([[dict objectForKey:#"status"] isEqualToString:#"Closed"]) {
That will work - but I bet you could find a more efficient way of conditionally changing color than checking a string inside cellForRowAtIndexPath. Trouble is this method will be called often. You are reusing cells, which is an efficiency measure, that's good. But then you do something relatively expensive like a string comparison - how about adding a BOOL that could be checked instead, and setting it when you decide on the cell's text?

Related

constant tableviewcell at the beginning of the tableview

I would like to know if there is any sneaky way of getting a UITableViewCell to appear at the beginning of a UITableView no matter what the array that is going to populate the tableview contains?
I am having issues where I would like to have a "select all" cell at the top of the tableview but currently having issues trying to adjust the arrays I have going into the tableView:cellForRowAtIndexPath:
Not sure of your end goal, but there are many ways some easier (sneaky ways) than others depending on your needs. It sounds like your items are fairly static, so you can just insert them before you display or update the table row.
say you have a datasource called self.items and you did something to get data. Maybe in your app you are round tripping to some data source based upon input, like sort selectors or from the search bar delegate.
try something like this in the area where you load your datasource.
NSString *myCustomObject = #"Jump To Songs";
self.items = [self getGetMovieList];
[self.items insertObject:myCustomObject atIndex:0];
[self.tableView reloadData];
Another easy way would be to add sections to your table and just make the first section your navigation items.
Note: you may need to handle the actions in didSelectRowAtIndexPath......
there you go, not so sneaky, but pretty simple.
be well
You can make two types of UITableViewCell, then return the "select all" one if indexPath.row == 0.
On the other hand, how about just make a UIView for the "select all" functionality, and set it as the tableHeaderView of your table view?

NSString UITextView without replacing previous text

How would I go about adding text to a UITextView without replacing the previous text?
So far I have a UITextView and a UIButton that adds the text to the UITextView, but I would like the text field to append more text every time you hit the button instead of completely deleting the text and replacing it.
Here are some ways to overcome obstacles in iOS development:
Look at the documentation for the particular class you're trying to manipulate. In this case, UITextView documentation can be found within Xcode or online.
Command-Click on UITextView or any other object anywhere in your code, and it will bring you to the header file for that class. The header file will list every public method and property.
Look at your existing code. I'm assuming that since you have a button that adds text to a UITextView, you understand how to set its text. 99% of the time you'll find that any setter (mutator) methods will have a corresponding getter (accessor) method. In this case, UITextView has a method called setText: and a matching method just called text.
Finally, NSString has a convenience method called stringWithFormat: that you can use to concatenate (join) two strings, among other very useful things. %# is the format specifier for a string. For example, to combine two strings, stringOne and stringTwo, you could do the following:
NSString *string = [NSString stringWithFormat:#"%# %#", stringOne, stringTwo];
I will leave you to come up with the answer as to how to combine NSString stringWithFormat: and UITextField text and setText: to achieve what you'd like to accomplish.
Edit:
The OP was unable to figure out how to utilize the information above so a complete code sample has been provided below.
Assume you have synthesized property (possibly an IBOutlet) UITextView that you have initialized called myTextView. Assume also that we are currently in the method scope of the method that gets called (your IBAction, if you're using IB) when you tap your UIButton.
[myTextView setText:[NSString stringWithFormat:#"%# %#", myTextView.text, #"this is some new text"]];
Explanation: myTextView.text grabs the existing text inside of the UITextView and then you simply append whatever string you want to it. So if the text view is originally populated with the text "Hello world" and you clicked the button three times, you would end up with the following progression:
Initial String: #"Hello world"
Tap one: #"Hello world this is some new text"
Tap Two: #"Hello world this is some new text this is some new text"
Tap Three: #"Hello world this is some new text this is some new text text this is some new text"
If all you are doing is appending text, you might find this a little simpler:
myTextView.text = [myTextView stringByAppendingString:#"suffix\n"];
I found this on UITextView insert text in the textview text. Sadly, I have not found a way to append text directly without a wholesale replacement of the text in the UITextView. It bugs me that the effort involved is proportional to the total length of the existing string and the suffix, rather than just the suffix.
A more efficient way to append text is to use replace() at the end:
extension UITextInput {
func append(_ string : String) {
let endOfDocument = self.endOfDocument
if let atEnd = self.textRange(from: endOfDocument, to: endOfDocument) {
self.replace(atEnd, withText: string)
}
}
}
#Jack Lawrence: Your answer doesn't cover the question completely.
The example below will not scroll neatly while running off the bottom when called every second:
self.consoleView.text = [NSString stringWithFormat:#"%#%#%#", self.consoleView.text, data, #"\n"];
[self.consoleView scrollRangeToVisible:NSMakeRange(self.consoleView.text.length, 0)];
This is caused by setText replacing the original text every time thereby resetting associated contentOffsets etc.
This was possible prior to iOS 7, but since iOS 7 it seems that setText cannot be prevented from exhibiting jumpy behaviour. Appending does not seem to be an option for TextViews in this scenario?

how to switch language at runtime inside the app?

I have a requirement to switch UI language at run-time by opening and selecting a popup menu. I did some reading on cocoa's internationalization but found it not suitable for my project since it requires user to make changes in the system setting.
Post like "http://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language" suggests setting the AppleLanguages would also require an app-restart.
I wonder if I can (a) create multiple NIBs and call initWithNib according to the current language or (b) use one NIB but create a plist of strings and make a function to set title string for all the texts in all my viewWillAppear. Which solution sounds better or please let me if there is a third way.
Thanks
Leo
The simplest way I can think of is reloading all your views, and updating the text property of all text components (such as UILabel, UITextField, etc.). The language specific text to be set can be returned by a method of yours, something like-
-(NSString*) localizedStringForKey: (NSString*)key andLanguage: (NSString*)lang
{
if([lang isEqualToString:#"en"])
{
if(key == #"hello")
return #"Hello";
//and so on...
}
else if([lang isEqualToString:#"fr"])
{
if(key == #"hello")
return #"Bonjour";
//and so on...
}
}
P.S: Here I am returning hard-coded values, but you can read them from language-specific plists of yours.

One label, two different fonts?

I need to format text in a label like this:
username: some text from this user. This will
create additional lines of text that will go
on and on and on.
Where "username" is bold. This will go into a UILabel, which is in a custom table cell. Is there a way to get this type of layout?
For this relatively simple case, you might be able to fake it. Have one label with the bold username, and another label with the plain text in the same position. Insert enough spaces before the plain text to leave room for the username. You can use UIStringDrawing methods to measure the bold text and the spaces.
CGSize usernameSize = [theUsername sizeWithFont:theBoldUsernameFont];
CGSize spaceSize = [#" " sizeWithFont:thePlainCommentFont];
NSString *indentedComment = [NSString stringWithFormat:#"%*s%#" , (int)ceil( usernameSize.width / spaceSize.width ) , "" , theComment];
If you use plain UILabel it's not available. Use two labels for this task.
You need to use either a UIWebView or CoreText to do this kind of advanced text layout. A web view has a lot of overhead but is most flexible and you can't use it effectively in a UITableView cell. CoreText is low level and not that well documented. You could ditch the table view and just lay out the table with CSS and HTML in the web view, which is how I do it.
You can still use a UITableViewCell but have the cell use a UIWebView subview. Set up a custom cell subclass with a clever setter method that allows you to send nsstrings to the method with turns those into a pretty formatted view.

Design cell make UITableView slow

Here is my problematique: I design my cell in my UItableView, so I added a title, a little description and an image.
All thoses element are store in my database, so in my UIViewController I calculate every position to have a nice cell, if there is no image in a cell I change the position of the title and the little description.
To check if the image is present or not I do something like that:
if ([fiche linkImg]!=#"") { //draw position of element }
or
if ([fiche.linkImg length] > 0 ) { //draw position of element }
My problem is when I begin to slide in my tableview its very slow and often very often crash, and the error are sometimes:
[CALayerArray listImg]
or
[NSCFArray listImg]:
Any idea?
Your description is a tiny bit confusing to see what is the problem.
I would suggest having a look at how to accomplish adding custom stuff to cell.
Have a read of apple's doc on UITableView especially a closer look at Table-View cells, if you scroll down in that section you will find examples of exactly the type of cells you are trying to create (text with pictures).
Try adding like this
if([fiche linkImg]!=nil)
I tried and succeeded