简体   繁体   English

根据选择器设置设置表格视图单元格的名称

[英]Setting the table view cell's name based on picker selection

I have made a table view with a button that adds cells. 我用添加单元格的按钮制作了一个表格视图。 I am new to this area of objective C so I have some questions. 我是目标C领域的新手,所以我有一些疑问。 I also have it so the cell can be tapped and a detailViewController is opened. 我也有它,因此可以点击单元格并打开detailViewController。 The detailViewController has a picker in it. detailViewController中有一个选择器。 How could I set the pickers selection to be the title of the tableViewCell? 如何将选择器选择设置为tableViewCell的标题? Their is going to be more than 1 table view cell, that will have to have different titles based on the pickers selection so do I need to make different views for each cell? 他们将有1个以上的表格视图单元格,根据选择器的选择,它们必须具有不同的标题,所以我需要为每个单元格设置不同的视图吗? Sorry if these questions are stupid, i'm just really new to this area. 很抱歉,如果这些问题很愚蠢,我只是这个领域的新手。


Edit 编辑

I have been doing my homework and came up with some code, but I am still getting an error. 我一直在做作业,想出了一些代码,但是仍然出现错误。 I have two views, one is a tableViewController, and one is the one with the picker. 我有两个视图,一个是tableViewController,一个是带有选择器的视图。 I have declared the cell and set its identifier to cell. 我已经声明了单元格并将其标识符设置为cell。 I then imported the .h file from my tableViewController to my pickerViewController. 然后,将.h文件从tableViewController导入到pickerViewController中。 But when I set the title, i get an error. 但是,当我设置标题时,出现错误。 The code for setting my title is 设置我的标题的代码是

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    int select = row;
    if (select == 0) {
        cell.textLabel.text = @"Title";
    } else

} But I am getting the error "Use of undeclared identifier cell". 但我收到错误消息“使用未声明的标识符单元格”。

In the .h file of the tableViewController has this UITableViewCell *cell; 在tableViewController的.h文件中,具有此UITableViewCell *cell; in it 在里面

And the .m file has this .m文件具有此

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

Also in the .m file of the pickerViewController I have this `#import "TableViewController.h" 同样在pickerViewController的.m文件中,我有这个`#import“ TableViewController.h”

Am I doing something wrong? 难道我做错了什么? should I be getting this error? 我应该得到这个错误吗? Sorry if this is hard to understand, because I had trouble turning my problem into words. 抱歉,这很难理解,因为我无法将问题转化为文字。

Your approach is not correct. 您的方法不正确。 You basically need the following steps. 您基本上需要执行以下步骤。

1) Your array or dictionary that you use to populate the table view needs to have a property (or key if it's a dictionary) for "title". 1)用于填充表格视图的数组或字典需要具有“标题”的属性(或键,如果是字典,则为键)。 You would use that to set the title in each cell. 您将使用它在每个单元格中设置标题。 It can be an empty string initially, if you don't set any titles until the user picks something from the picker 如果您在用户从选择器中选择内容之前未设置任何标题,则最初可以为空字符串

2) When you open the detail view controller, you should keep a reference to the selected row in a property (in the table view controller). 2)打开明细视图控制器时,应在属性中保留对选定行的引用(在表视图控制器中)。 If you're doing the transition in code, this would be in tableView:didSelectRowAtIndexPath:, if you're using segues it should be done in prepareForSegue:. 如果您要在代码中进行转换,则它将位于tableView:didSelectRowAtIndexPath:中,如果您使用segues,则应在prepareForSegue:中完成。 The controller should also set itself as the delegate of the detail controller in one of these two methods. 在这两种方法之一中,控制器还应将自己设置为详细信息控制器的委托。

3) The detail controller should have a delegate protocol with one method that passes the title back to the table view controller. 3)详细信息控制器应具有一种委托协议,该协议具有一种将标题传递回表视图控制器的方法。 After the user makes a selection in the picker, call the delegate method. 用户在选择器中进行选择后,调用委托方法。 The table view controller would implement this method and use the row information stored in its property to add that title string to the array, and then update the table view. 表格视图控制器将实现此方法,并使用存储在其属性中的行信息将标题字符串添加到数组,然后更新表格视图。

When your cell is tapped, you are using the 轻按单元格时,您正在使用

- (UITableViewCell *)tableView:(UITableView *)tableView 
       didSelectRowAtIndexPath:(NSIndexPath *)indexPath

method to show your UIPickerView, right? 显示UIPickerView的方法,对不对? You will need to access this selected row/cell whenever your pickerViewDidSelectRow function is called, in order to change the cell's title. 每当更改pickerViewDidSelectRow函数时,都需要访问此选定的行/单元格,以更改单元格的标题。 There are a bunch of ways you can do this. 您可以通过多种方法来执行此操作。

The way that is closest to the way you have been trying to implement is to save a pointer to the selected cell in the tableViewDidSelectRowAtIndexPath method with: 与您尝试实现的方法最接近的方法是使用以下方法在tableViewDidSelectRowAtIndexPath方法中保存指向所选单元格的指针:

self.selectedCell = [tableView:tableView cellForRowAtIndexPath:indexPath];

For this to work, you will need to change the name of your 'cell' in .h file to 'selectedCell'. 为此,您需要将.h文件中“单元”的名称更改为“ selectedCell”。 I suggest this change as 'cell' can be confusing. 我建议将此更改称为“单元格”可能会造成混淆。

Then, in your pickerViewDidSelectRowInComponent, you will set the title of your selected cell like this: 然后,在pickerViewDidSelectRowInComponent中,将设置选定单元格的标题,如下所示:

self.selectedCell.textLabel.text = @"Title";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM