有人可以给我看一个UITableView单元的就地编辑示例...我知道UITableView委托方法,如cellForRowAtIndexPath,...等
但我不知道如何允许单元格的就地文本编辑? 此值也可以与Core Data一起使用,即它可以保留。
我正在寻找的东西可以在设置 - >下看到。 Wi-fi,您可以在其中看到域,客户端,IP等字段,其中值可以在同一位置设置。
使用具有单独视图控制器的就地编辑V来控制字段值是否有任何缺点?
答案 0 :(得分:18)
向您的单元格添加UITextField
。
无论您选择编辑条目,无论是使用CoreData还是存储信息,您都需要保存它。如果您使用on-Table编辑方法,则可以使用textField委托在用户返回时保存数据。
在cellForRowAtIndexPath
:
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,10,125,25)];
myTextField.adjustsFontSizeToFitWidth = NO;
myTextField.backgroundColor = [UIColor clearColor];
myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
myTextField.textAlignment = UITextAlignmentRight;
myTextField.keyboardType = UIKeyboardTypeDefault;
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.clearButtonMode = UITextFieldViewModeNever;
myTextField.delegate = self;
cell.accessoryView = myTextField;
TextField Delegates
:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField == myTextField){
/* do your saving here */
}
}
答案 1 :(得分:0)
这是非常古老的..并且可能需要一个更近期的答案..我只是遇到了这个问题并且一起攻击了一些东西......也许有人发现它很有用..
我创建了一个表视图控制器,它添加了一个消息框,添加了存储在NSUserDefaults中的“项目列表”的结尾......项目
//
// MyItemListTVC.m
// Best10
//
// Created by Francois Chaubard on 12/26/13.
// Copyright (c) 2013 Chaubard. All rights reserved.
//
#import "MyItemListTVC.h"
#import "AppDelegate.h"
@interface MyItemListTVC ()
@property (strong, nonatomic) UIRefreshControl IBOutlet *refreshControl;
@property (strong,nonatomic) UIBarButtonItem *addButton;
@property (strong,nonatomic) UITextView *messageBox;
@end
@implementation MyItemListTVC
@synthesize refreshControl;
@synthesize itemList;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.itemList=[(AppDelegate *)[UIApplication sharedApplication].delegate getitems];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
self.addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.addButton.enabled = false;
//UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save:)];
[self.navigationItem setRightBarButtonItems:@[self.addButton] animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)__unused sender
{
NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:self.itemList];
self.itemList = nil;
[self.tableView reloadData];
[temp addObject:self.messageBox.text];
[[NSUserDefaults standardUserDefaults] setObject:temp forKey:@"items"];
self.itemList=[(AppDelegate *)[UIApplication sharedApplication].delegate getitems];
self.messageBox = nil;
self.addButton.enabled = NO;
[self.tableView reloadData];
[self.tableView setNeedsDisplay];
[CATransaction flush];
}
- (void) save:(id)__unused sender {
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)__unused tableView numberOfRowsInSection:(NSInteger)section
{
return [self.itemList count]+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (indexPath.row ==[self.itemList count] ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MessageBox Cell"];
UITextView *messageBox= [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 100)];
cell.userInteractionEnabled=YES;
messageBox.delegate=self;
[messageBox setEditable:YES];
[messageBox setUserInteractionEnabled:YES];
messageBox.editable=YES;
messageBox.font = cell.textLabel.font;
messageBox.textAlignment = NSTextAlignmentCenter;
messageBox.textColor = [UIColor grayColor];
messageBox.text = @"insert new activity";
self.messageBox = messageBox;
[cell addSubview: messageBox];
}else{
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
}
return cell;
}
- (BOOL)tableView:(UITableView *)__unused tableView canEditRowAtIndexPath:(NSIndexPath *)__unused indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)__unused tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((editingStyle == UITableViewCellEditingStyleDelete)&&([self.itemList count]>indexPath.row)) {
NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:self.itemList];
[temp removeObjectAtIndex:indexPath.row];
[[NSUserDefaults standardUserDefaults] setObject:temp forKey:@"items"];
self.itemList=[(AppDelegate *)[UIApplication sharedApplication].delegate getitems];
[self resignFirstResponder];
[self.tableView reloadData];
}
}
- (BOOL)tableView:(UITableView *)__unused tableView canMoveRowAtIndexPath:(NSIndexPath *)__unused indexPath
{
// The table view should not be re-orderable.
return YES;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
if ([self.itemList count] > 0){
cell.textLabel.text = [self.itemList objectAtIndex:indexPath.row];
}
}
#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
textView.text = @"-ing";
[self.messageBox setSelectedTextRange:[self.messageBox textRangeFromPosition:[self.messageBox positionFromPosition:self.messageBox.beginningOfDocument offset:1] toPosition:self.messageBox.beginningOfDocument]];
}
-(void)textViewDidChange:(UITextView *)textView
{
if (textView.text.length > 4) {
self.addButton.enabled=YES;
}else{
self.addButton.enabled=NO;
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((indexPath.row ==[self.itemList count] ) ) {
return UITableViewCellEditingStyleNone;
}else{
return UITableViewCellEditingStyleDelete;
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
if (textView.text.length > 4) {
[self insertNewObject:textView.text];
}
[self resignFirstResponder];
return NO; // or true, whetever you's like
}else if((textView.text.length-range.location)<4){
return NO;
}
if (textView.text.length > 4) {
self.addButton.enabled=YES;
}else{
self.addButton.enabled=NO;
}
return YES;
}
@end