2014-01-23 50 views
-2

這是我的問題。我創建了一個視圖控制器,並在其中插入了一個表格視圖(包含一個單元格)。我在視圖控制器的底部也有一個文本字段。目標是將單元格寫入文本字段中。我嘗試了很多東西,但沒有結果。 任何人都可以幫助我嗎?在視圖控制器中設置表格視圖

這裏是我的代碼(我無法顯示的NSLog)

My .h 
@interface RBChatViewController : UIViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource> 


@property (strong, nonatomic) IBOutlet UITextField *entryTextField; 
@property (strong, nonatomic) IBOutlet UITableView *tableview; 

My .m 
#import "RBChatViewController.h" 

@interface RBChatViewController() 

@end 

@implementation RBChatViewController 


- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
self.entryTextField.delegate=self; 
self.tableview.delegate=self; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    NSLog(@" numberofsections"); 
    return 1; 
} 
+0

您需要發佈您嘗試過的內容並解釋您遇到的問題。 – rmaddy

回答

0

很難告訴你發生了什麼事,卻沒有看到你的代碼,但這段代碼應該工作。

-(void)addTextToTable { 
    //method runs when you push a button to say i am done writing in text field 

    NSString *textFeildText = self.textFeild.text; //create string of textfeild text 
    [self.mutableArray addObject:textFeildText]; //add string to dataSource array 
    [self.table reloadData]; // update table with new data to show the new string that is created 
} 

編輯以下是您需要添加的內容。

 My .h 
    @interface RBChatViewController :       UIViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource> 


    @property (strong, nonatomic) IBOutlet UITextField *entryTextField; 
    @property (strong, nonatomic) IBOutlet UITableView *tableview; 
    @property (nonatomic) NSMutableArray *mutArray; // array of object that will be in the tableview 

    My .m 
    #import "RBChatViewController.h" 

    @interface RBChatViewController() 

    @end 

    @implementation RBChatViewController 


    - (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
    self.entryTextField.delegate=self; 
    self.tableview.delegate=self; 
    self.tableview.dataDelegate = self; // ADD THIS, this means that the data for the   tableview is gather from here This will also display your NSLog 
    _mutArray = [[NSMutableArray alloc] init]; //create your mutable array 
    } 

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
     NSLog(@" numberofsections"); 
     return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     //tells how many rows in your tableview based on object in array 
     return _mutArray.count; 

    } 




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

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

     NSString *string = [_mutArray objectAtIndex:indexPath.row]; 

     cell.textLabel.text = string; 


    return cell; 

    } 
+0

它的工作!謝謝!! – Zoomzoom

+0

沒問題。但是你知道它爲什麼現在在工作嗎? – Charlie

+0

是的,我忘了self.tableview.datasource = self; – Zoomzoom

0

你應該有tableView和textField屬性然後, 必須implment的tableViewdelegatedatasource方法。並在cellForRowAtIndexPath中設置cell.titleLabel.text = self.textField.text,並從textFieldDidChangeText中調用[tableView reloadata];,或者如果您有一些按鈕在該按鈕操作中添加重載代碼。