2015-04-06 91 views
0

我有一个按钮,一个pickerview和一个tableview。当我按下按钮时,pickerview的当前选择将被填充到表格视图中。下面是按钮的其中挑选值从pickerview使用pickerview填充tableview

- (IBAction)addCourse:(UIButton *)sender { 

    NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc 
    NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer 
    NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent]; 

    NSString *num=Number[numRow]; 
    NSString *season=Season[SeaRow]; 
    NSString *course=Course[CourseRow]; 

    NSString *msgCourse=[[NSString alloc ]initWithFormat:@"%@ ",course]; 
    NSString *msgSeason=[[NSString alloc ]initWithFormat:@"%@ ",season]; 
    NSString *msgYear=[[NSString alloc ]initWithFormat:@"%@ ",num]; 

} 

然后我想填充msgCourse,等我的tableview代码

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

...Content from above msgCourse and etc 

    return cell; 

} 

如何解决在我的第二个部分的差距吗?或者请看一下任何示例?

回答

1

请检查以下代码,根据我对您问题的理解,确定是否符合您的要求。如果没有,请留下评论,然后我会尽我所能跟进。

第一步:,如果您还没有通过故事板代表团,这里是你应该编程做的第一件事:

-(void)ViewDidLoad 
    { 
    tableView.delegate = self; 
    tableView.datasource = self; 
    } 

第二步:我还没有见过mutablearray您代码,但我假设你将使msgCourse到NSMutableArray。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// I assume you have only one section 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

// Return the number of rows in the section. 
return [self.msgCourse count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = nil; 
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    if(!cell){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; 
     } 

    cell.textLabel.text=[self.msgCourse objectAtIndex:indexPath.row]; 
    return cell; 
} 

最后一步:对了,不要忘记下面的代码行添加到您的按钮操作刷新你的tableView,我假设你更新的NSMutableArray,并希望刷新的tableView。

- (IBAction)addCourse:(UIButton *)sender { 
    ..... 
    ..... 
    ..... 
    [tableView reloadData]; 
} 

这里是很好的教程,这是值得重复学习的缘故:http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/

+0

非常感谢您的回复。它报告错误cell.textLabel.text = msgCourse;我认为msgCourse是一个局部变量?再次感谢。 – user4441082

+0

你应该让它成为全球性的。 – casillas

+0

感谢您的耐心等待。如果我需要在多行tableview中填充多条消息,是否需要修复某些内容? – user4441082