JavaEar 专注于收集分享传播有价值的技术资料

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?

2个回答

    最佳答案
  1. 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

  2. 参考答案2
  3. @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.