Skip to content

Instantly share code, notes, and snippets.

@serhatsezer
Last active August 29, 2015 14:08
Show Gist options
  • Save serhatsezer/10ff59eacb68e4f8a670 to your computer and use it in GitHub Desktop.
Save serhatsezer/10ff59eacb68e4f8a670 to your computer and use it in GitHub Desktop.
Tableview insert new row
//
// TableViewController.m
// tabbar_sample
//
// Created by Serhat Sezer on 24/10/14.
// Copyright (c) 2014 SecretCV. All rights reserved.
//
#import "TableViewController.h"
@interface TableViewController ()
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic,assign) BOOL isModified;
@property (nonatomic,assign) NSInteger editedRowNum;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setContentInset:UIEdgeInsetsMake(20, 0, 0, 0)];
self.array = @[].mutableCopy;
self.editedRowNum = 0;
self.tableView.tableFooterView = [[UIView alloc] init];
[self performSelector:@selector(appendSomeData) withObject:nil afterDelay:1.5];
}
- (void)appendNewRow {
// yeni boş bir row ekliyoruz.
[self.array addObject:@""];
// tekrardan düzelendiğini belirtiyoruz.
self.isModified = YES;
[self.tableView beginUpdates];
NSIndexPath *indexPathForNewRow = [NSIndexPath indexPathForRow:self.editedRowNum inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPathForNewRow] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
- (void) appendSomeData {
[self.array addObject:@"a"];
[self.array addObject:@"b"];
[self.array addObject:@"c"];
[self.array addObject:@"d"];
[self.array addObject:@"e"];
[self.array addObject:@"f"];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
[self performSelector:@selector(appendNewRow) withObject:nil afterDelay:2.0];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"array count %d ", self.array.count);
return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.array[indexPath.row];
// with textfield
if(indexPath.row == self.editedRowNum && self.isModified) {
cell.textLabel.text = @"";
UITextField *textFieldForCell = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, 200, 50)];
textFieldForCell.placeholder = @"Enter your name here!";
[cell addSubview:textFieldForCell];
}
return cell;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment