ios - TextView 数组到 TableView

标签 ios objective-c uitableview

我需要使用 UITextView 输入信息,然后将信息移动到 UITableView 中。

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

每次添加新行时,我都需要它来更新表格 View 。

我想我可以根据 TextView 中的信息创建一个数组并将其放入 TableView,但我似乎无法弄清楚如何做到这一点。

有了这个,它仍然不会用我在“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;
}

最佳答案

enter image description here

首先将UITextViewdelegate 属性设置为您的viewController。然后使用 textViewDidChange 方法来初始化你的数组——

ViewController.h 文件中-

    #import <UIKit/UIKit.h>

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

ViewController.m 文件中

#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

使用 listArraytableView 上显示您的数据。

关于ios - TextView 数组到 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36127579/

相关文章:

ios - 如何在 UIDatePickerView 中隐藏 future 或过去的日期

ios - 录制音频时捕获,停止接收音频 AVAudioRecorder

ios - Swift 调用中的额外参数 'predicate'

ios - UILabel 为零

ios - 递归调用 block 时的 EXC_BAD_ACCESS

objective-c - '无法使标识符为 <addressCell> 的单元格出列

ios - 为什么自动调整表格 View 单元格中的 UIImageView 具有内容模式 AspectFit 的原始高度?

objective-c - 在coredata和restkit中本地更新托管对象的属性

ios - Apple 推送通知不适用于临时构建

ios - 如何为 iPhone/iPad 创建纵向 iAd 横幅 View