1

I need to use a UITextView to enter in information, which then moves the information into a UITableView.

This is just an example of what i want it to look like.[1]

I need it to update the tableview each time a new line has been added.

I thought i could create an array out of the information in the TextView and place it into a TableView but i cant seem to figure out how to do this.

With this it still doesn't populate the table with what i type in the 'UITextView'.

-(void)textViewDidChange:(UITextView *)textView{

listArray=[[NSMutableArray alloc]initWithArray:[textView.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];

[self.tableView reloadData];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [listArray objectAtIndex:indexPath.row];

return cell;
}
3
  • Please post your code here. Commented Mar 21, 2016 at 9:52
  • Can you please update your question with code so easily to slow your problem. Commented Mar 21, 2016 at 10:09
  • The question i am asking is the first stage of my project, the above image is just an example i made using an array and a textview. Commented Mar 21, 2016 at 10:49

3 Answers 3

1

enter image description here

First set the delegate property of the UITextView to your viewController. Then use textViewDidChange method to initialize your array--

in ViewController.h file-

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    @property (weak, nonatomic) IBOutlet UITextView *txtView;
    @end

and in ViewController.m file

#import "ViewController.h"

@interface ViewController (){

    NSMutableArray *listArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.txtView.delegate = self;
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    cell.textLabel.text = [NSString stringWithFormat:@"%@",[listArray objectAtIndex:indexPath.row]];;

    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return listArray.count;
}

-(void)textViewDidChange:(UITextView *)textView{

    listArray=[[NSMutableArray alloc]initWithArray:[textView.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
    [self.tableView reloadData];

    NSLog(@"Array-- %@",listArray);

    NSLog(@"Array Count-- %lu",(unsigned long)listArray.count);

    NSLog(@"Text-- %@",textView.text);

}
@end

Use listArray to show your data on tableView.

Sign up to request clarification or add additional context in comments.

29 Comments

The data entered in the textview does not fill the tableview still.
have to add UITextViewDelegate in .h file- @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>
also set delegate and datasource of your tableview to your viewController.
Still no luck, tableview just opens blank
can you share your code, so that i can clearly understand your problem. This code works fine according to your problem.
|
0

You can create an array with the text of textview using the following method :

NSString* string;
NSArray* array = [string componentsSeparatedByString:(nonnull NSString*)]

now you can create your tableview using the above array. whenever you want to update the table, you can call the following method :

[tableView reloadData];

Comments

0

If you just want to look for new lines you can do something like this:

NSArray <NSString*> *components = [self.textView.text componentsSeparatedByString:@"\n"];

If you implement the delegate methods of UITextView specifically to get notifications when the text content changes you can then update an array that feeds into your table view.

Also here's the code in Swift just because we should be thinking about it now.

let components: [String] = textView.text.componentsSeparatedByString("\n")

2 Comments

I prefer to use .componentsSeparatedByCharactersInSet(NSCharacterSet. newlineCharacterSet()), but I'm allergic to hardcoded values.
Fair enough. That is definitely better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.