就地的UITableView细胞的编辑
问题描述:
可有人请告诉我在位的UITableView细胞的编辑的例子......我知道的UITableView委托方法类似的cellForRowAtIndexPath,...等就地的UITableView细胞的编辑
但我不知道如何允许对单元格进行原地文本编辑?
也可以使用此值与核心数据,即它可以坚持..
我正在寻找的东西可以看到在设置 - >无线网络,你看到这些领域域,客户端,IP等等,这些值可以在同一个地方设置。
也有使用就地编辑Vs有一个单独的视图控制器来控制字段值的任何缺点?
答
将一个UITextField
添加到您的手机。
无论选择哪种方式编辑条目,无论是使用CoreData还是其他存储信息的方法,都需要进行保存。如果您使用在表编辑方法,则可以使用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 */
}
}
答
这是很老..大概需要一个更近的答案..我只是有这个问题,黑客攻击一起东西..也许有人认为它有用..
我创建了一个表视图控制器,添加一个消息框添加存储在NSUserDefau中的“项目列表”的结尾... ...项目
//
// 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
虽然你似乎已经得到它在我正在寻找的权利,我仍然有几个问题,如什么应该是表格的单元格样式(如Val1,Val2等) )。 accessoryView与accessoryType相同吗? 也可以这种方法只用于就地文本编辑或我可以用它来编辑其他类型,如说一个日期? – hmthur 2010-12-19 18:39:23
默认样式很好,因为你不会有任何detailText。 AccessoryView可以用来replcae acessoryType,所以不用Disclusre按钮,你可以用TextField替换它。所以你不需要一个accessoryType。您可以使用此方法编辑日期,数字,文本,任何您能想到的内容。 – WrightsCS 2010-12-19 18:46:47