如何使单元格可编辑

时间:2013-08-05 11:14:54

标签: ios objective-c

我目前正在尝试让用户能够编辑多个单元格的内容。更新后,数据将发送到Web服务。

好了,就我所读,只有"删除"和"添加"行(S)。我似乎无法找到有关如何编辑单元格内容的任何指南或教程。

非常感谢您的建议和/或建议。

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   

1 个答案:

答案 0 :(得分:3)

您无法直接编辑单元格内容。如果您需要编辑内容,请在单元格中添加UITextFieldUITextView作为其子视图。然后访问它们。

编辑:您可以添加如下文字字段:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        // ADD YOUR TEXTFIELD HERE
        UITextField *yourTf = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, 330.0f, 30)];
        [yourTf setBackgroundColor:[UIColor clearColor]];
        yourTf.tag = 1;
        yourTf.font = [UIFont fontWithName:@"Helvetica" size:15];
        yourTf.textColor = [UIColor colorWithRed:61.0f/255.0f green:61.0f/255.0f blue:61.0f/255.0f alpha:1.0f];
        yourTf.delegate = self;
        [cell addSubview:yourTf];

    }

    // ACCESS YOUR TEXTFIELD BY REUSING IT
    [(UITextField *)[cell viewWithTag:1] setText:@"YOUR TEXT"];

    return cell;
}

实现UITextField委托,然后您可以使用此UITextField编辑单元格内容。

希望它对你有所帮助。

相关问题