Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 添加带有自定义文本的新UITableView行_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 添加带有自定义文本的新UITableView行

Ios 添加带有自定义文本的新UITableView行,ios,objective-c,uitableview,Ios,Objective C,Uitableview,使用此代码 - (IBAction)testAdd:(id)sender { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.numberOfRows inSection:0]; [self.tableView beginUpdates]; self.numberOfRows++; [self.tableView insertRowsAtIndexPaths:@[indexPath]with

使用此代码

- (IBAction)testAdd:(id)sender 
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.numberOfRows inSection:0];

    [self.tableView beginUpdates];
    self.numberOfRows++;
    [self.tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView endUpdates];


}
我可以通过应用程序上的“添加”按钮向tableView添加新项目。这基本上会添加一个与前面表中已存在的项相同的项。 例如,我有一个tableview,第一行显示一个字符串“TEST”,点击add会添加另一行显示“TEST”。 我希望能够为新行传入一个自定义值,因此点击add将输出一行,并显示“NEWTHING”

我的数据源实际上是另一个视图控制器,它接受用户输入并将其发送到我的tabelViewController,该项的文本为“val2”。
我真正想要实现的是点击add,返回用户输入视图控制器,获取新数据并将其发送回我的tableViewController以显示

您要求的是在
-cellforrowatinexpath:
(大多数情况下,这取决于您设计数据源的方式)但如果这对您来说无关紧要,那么您可以:

- (IBAction)testAdd:(id)sender 
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.numberOfRows
                                                inSection:0];
    self.numberOfRows++;

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[indexPath]
                          withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView endUpdates];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [cell.textLabel setText:@"NEWTHING"];
}
但请注意,当您向上/向下滚动很远并返回到此单元格时,它很可能会显示“
TEST
”(
-cellforrowatinexpath:
将显示它的真正用途)

PS:如果要继续,请在问题中包括您的
-cellforrowatinexpath:
方法实现


编辑: 您的
-cellforrowatinedexpath
太静态了……从这个意义上讲,它只是将
self.val2
设置为
cell.textlab

假设从10行开始,
-cellforrowatinexpath
将被调用10次,每次,它都会将
self.val2
设置到当前单元格的
文本标签上
现在…当您添加一行(点击按钮)时,将为第11个单元格调用
-cellforrowatinexpath
,并为其设置相同的*文本。
*这在技术上是发生的,但我们很快改变了手机的文字

基本上,
tableView
不知道如何区分现有单元格和新添加的单元格,因为数据源本身不是动态的

为了指导
tableView
如何处理不同的单元格,我们需要创建一个更动态的数据源

有不同的方法可以使用,但我通常会这样做:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.val2 = @"TEST";

    //declare "NSMutableArray *arrDatasource;" globally
    //this will be the soul of the tableView
    arrDatasource = [[NSMutableArray alloc] init];

    int i_numberOfCells = 10;

    //populate beginning cells with default text
    for (int i = 0; i < i_numberOfCells; i++) {
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
        [dictionary setObject:self.val2 forKey:@"displayText"];

        [arrDatasource addObject:dictionary];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return number of objects in arrDatasource
    return arrDatasource.count;
}

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

    //pick up value for key "displayText" and set it onto the cell's label
    [cell.textLabel setText:arrDatasource[indexPath.row][@"displayText"]];
    //this will be dynamic in nature because you can modify the contents
    //of arrDatasource and simply tell tableView to update appropriately

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //make indexPath of new cell to be created
    NSIndexPath *indexPathNEXT = [NSIndexPath indexPathForItem:arrDatasource.count inSection:0];

    //add the appropriate contents to a dictionary
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:@"NEWTHING" forKey:@"displayText"];

    //add the dictionary object to the main array which is the datasource
    [arrDatasource addObject:dictionary];

    //add it to tableView
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[indexPathNEXT]
                          withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    //this ends up calling -cellForRowAtIndexPath for the newly created cell
    //-cellForRowAtIndexPath shows the text (you put in the dictionary in this method above)
}
-(void)viewDidLoad
{
[超级视图下载];
self.val2=@“测试”;
//全局声明“NSMutableArray*arrDatasource;”
//这将是tableView的灵魂
arrDatasource=[[NSMutableArray alloc]init];
int i_numberOfCells=10;
//用默认文本填充起始单元格
对于(int i=0;i

PS:
-cellforrowatinexpath:
在单元格更新、刷新或需要显示时被调用,因此此方法需要正确实现

您可以向下滚动并返回重置文本。我将包括我的-cellforrowatinexpath:@Marcus:eeks,您的
-cellforrowatinexpath:
太静态。您需要取消设置签署一个更可爱的数据源。我将更新我的答案,以建议way@Marcus:很乐意帮忙。嗯……你可以从这里开始:)
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.val2 = @"TEST";

    //declare "NSMutableArray *arrDatasource;" globally
    //this will be the soul of the tableView
    arrDatasource = [[NSMutableArray alloc] init];

    int i_numberOfCells = 10;

    //populate beginning cells with default text
    for (int i = 0; i < i_numberOfCells; i++) {
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
        [dictionary setObject:self.val2 forKey:@"displayText"];

        [arrDatasource addObject:dictionary];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return number of objects in arrDatasource
    return arrDatasource.count;
}

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

    //pick up value for key "displayText" and set it onto the cell's label
    [cell.textLabel setText:arrDatasource[indexPath.row][@"displayText"]];
    //this will be dynamic in nature because you can modify the contents
    //of arrDatasource and simply tell tableView to update appropriately

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //make indexPath of new cell to be created
    NSIndexPath *indexPathNEXT = [NSIndexPath indexPathForItem:arrDatasource.count inSection:0];

    //add the appropriate contents to a dictionary
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:@"NEWTHING" forKey:@"displayText"];

    //add the dictionary object to the main array which is the datasource
    [arrDatasource addObject:dictionary];

    //add it to tableView
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[indexPathNEXT]
                          withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    //this ends up calling -cellForRowAtIndexPath for the newly created cell
    //-cellForRowAtIndexPath shows the text (you put in the dictionary in this method above)
}