Ted
Ted

Reputation: 3885

Need a view that acts like a log view

I need to implement a view that acts as a log view, so that when you push a message into it, the message would push other messages upwards. Is there anything like that for iOS?

Upvotes: 2

Views: 78

Answers (2)

Mick MacCallum
Mick MacCallum

Reputation: 130212

@Vladimir's answer is probably the way to go, but just for the sake of seeing some additional options, here's an example using a UITextView:

- (IBAction)addNewLog:(UIButton *)sender {
    NSString *myInputText = @"some new text from string";
    NSString *temp = myTextView.text;
    [myTextView setText:[temp stringByAppendingString:[NSString stringWithFormat:@"\n%@: %@",[NSDate date],myInputText]]];
    [myTextView setContentOffset:CGPointMake(0, myTextView.contentSize.height - myTextView.frame.size.height) animated:NO];

}

Then if you wanted to separate the text in the text view into objects in an array:

NSArray *myAwesomeArray = [myTextView.text componentsSeparatedByString:@"\n"];

Mind you, the above would break if the "myInputText" string ever contained a line break.

Upvotes: 1

Vladimir
Vladimir

Reputation: 170859

You can easily implement that using standard UITableView:

  • Each cell will be responsible for displaying 1 log message
  • Add new cell to the end of the table when new message arrive
  • Scroll table to the bottom after cell is added (using scrollToRowAtIndexPath:atScrollPosition:animated: method with UITableViewScrollPositionBottom position parameter)

That means you'll need to store your log messages in array, but if you're going to display them you need to store messages anyway

Upvotes: 3

Related Questions