《iOS总结》UITableViewCell的增加与删除-MickyChiang

来源:互联网 发布:qq的smtp服务器端口 编辑:程序博客网 时间:2024/06/06 02:05

UITableViewCell的增加与删除


我把代码都贴进来 看不懂的可以联系我= = 


// MyCell.h

#import <UIKit/UIKit.h>@interface MyCell : UITableViewCell @property (strong, nonatomic) IBOutlet UITextField *titleTextField;@property (strong, nonatomic) IBOutlet UITextView *contentTextView;@property (strong, nonatomic) IBOutlet UIButton *removeButton;@end

// MyList.h

#import <Foundation/Foundation.h>@interface MyList : NSObject@property (nonatomic, copy) NSString *title;@property (nonatomic, copy) NSString *content;@end

// ViewController.h
#import "ViewController.h"#import "MyCell.h"#import "MyList.h"@interface ViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UITextViewDelegate>@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) NSMutableArray *objectArr;@property (nonatomic, strong) MyCell *myCell;@end@implementation ViewController- (void)dealloc{    _tableView.delegate=nil;    _tableView.dataSource=nil;}- (void)viewDidLoad {    [super viewDidLoad];    [self change];}- (void)change{    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {        self.navigationController.navigationBar.translucent = NO;    }    self.title=@"tableview的增删改查";    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"保存" style:(UIBarButtonItemStylePlain) target:self action:@selector(saveCell:)];    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStylePlain) target:self action:@selector(insertCell:)];    MyList *myList = [[MyList alloc] init];    myList.title=@"1";    myList.content=@"1";    [self.objectArr addObject:myList];    self.tableView.backgroundColor=[UIColor whiteColor];}- (void)removeCell:(UIBarButtonItem *)item{    if (self.objectArr.count==1) {        if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=8.0)) {            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"不能全部删除" preferredStyle:(UIAlertControllerStyleAlert)];            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {            }];            [alert addAction:otherAction];            [self presentViewController:alert animated:YES completion:nil];        } else {            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能全部删除" delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了", nil];            [alert show];        }    } else {        NSLog(@"删除一个cell");        [self.objectArr removeObjectAtIndex:self.objectArr.count-1];        NSLog(@"self.objectArr.count:%@", self.objectArr);        NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];        [self.tableView deleteRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationFade];        [self.tableView reloadData];    }}- (void)saveCell:(UIBarButtonItem *)item{    NSLog(@"保存cell中的所有内容");    for (int i=0; i<self.objectArr.count; i++) {        if ([[[self.objectArr objectAtIndex:i] title] isEqualToString:@""] && [[[self.objectArr objectAtIndex:i] content] isEqualToString:@""]) {            [self.objectArr removeObjectAtIndex:i];        }    }    NSLog(@"保存后的self.objectArr:%@", self.objectArr);}- (void)insertCell:(UIBarButtonItem *)item{    NSLog(@"添加一个cell");    MyList *myList = [[MyList alloc] init];    myList.title=[NSString stringWithFormat:@"%ld", self.objectArr.count+1];    myList.content=[NSString stringWithFormat:@"%ld", self.objectArr.count+1];    [self.objectArr insertObject:myList atIndex:self.objectArr.count];    NSLog(@"self.objectArr:%@", self.objectArr);    NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];    [self.tableView insertRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationTop];    [self.tableView reloadData];}- (NSMutableArray *)objectArr{    if (_objectArr==nil) {        _objectArr=[NSMutableArray array];    }    return _objectArr;}- (UITableView *)tableView{    if (!_tableView) {        _tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];        _tableView.delegate=self;        _tableView.dataSource=self;        [self.view addSubview:_tableView];    }    return _tableView;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.objectArr.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{//    return 100;    MyCell *cell = (MyCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];    return cell.frame.size.height;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *CellIdentifier = [NSString stringWithFormat:@"MyCell-%ld-%ld",indexPath.section,indexPath.row];    [_tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    MyList *myList=[self.objectArr objectAtIndex:indexPath.row];    cell.titleTextField.text=myList.title;    cell.removeButton.tag=(indexPath.section+1)*1000+indexPath.row;    [cell.removeButton addTarget:self action:@selector(removeClicked:) forControlEvents:(UIControlEventTouchUpInside)];    cell.titleTextField.delegate=self;    cell.titleTextField.tag=(indexPath.section+1)*1000+indexPath.row;    cell.tag=(indexPath.section+1)*1000+indexPath.row;    cell.contentTextView.delegate=self;    cell.contentTextView.text=myList.content;    cell.contentTextView.tag=(indexPath.section+1)*1000+indexPath.row;    return cell;} - (BOOL)textViewShouldEndEditing:(UITextView *)textView{    NSLog(@"textViewShouldEndEditing");    NSLog(@"cell.contentTextView.tag:%ld", textView.tag);   [textView resignFirstResponder];    return YES;} - (void)textViewDidEndEditing:(UITextView *)textView{    NSLog(@"textViewDidEndEditing");    NSLog(@"cell.contentTextView.tag:%ld", textView.tag);    MyList *myList=[self.objectArr objectAtIndex:textView.tag-1000];    if (![textView.text isEqualToString:myList.content]) {        myList.content=textView.text;    } else {        NSLog(@"content的取值不变");    }}- (void)textFieldDidBeginEditing:(UITextField *)textField{    NSLog(@"textFieldDidBeginEditing");    NSLog(@"cell.titleTextField.tag:%ld", textField.tag);}- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{    NSLog(@"textFieldShouldEndEditing");    NSLog(@"cell.titleTextField.tag:%ld", textField.tag);    return YES;} - (void)textFieldDidEndEditing:(UITextField *)textField{    NSLog(@"textFieldDidEndEditing");    NSLog(@"cell.titleTextField.tag:%ld", textField.tag);    MyList *myList=[self.objectArr objectAtIndex:textField.tag-1000];    if (![textField.text isEqualToString:myList.title]) {        myList.title=textField.text;    } else {        NSLog(@"title的取值不变");    }} - (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}#pragma mark - 删除cell- (void)removeClicked:(UIButton *)button{    NSLog(@"buttonTag:%ld", button.tag);    if (self.objectArr.count==1) {        if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=8.0)) {            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"不能全部删除" preferredStyle:(UIAlertControllerStyleAlert)];            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {            }];            [alert addAction:otherAction];            [self presentViewController:alert animated:YES completion:nil];        } else {            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能全部删除" delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了", nil];            [alert show];        }    } else {        NSLog(@"删除一个cell");        [self.objectArr removeObjectAtIndex:button.tag-1000];        NSLog(@"self.objectArr.count:%@", self.objectArr);        NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];        [self.tableView deleteRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationFade];        [self.tableView reloadData];    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

// 运行后的效果

图一:


图二/图三: 

    

图四:






0 0
原创粉丝点击