示例代码(一共4份)
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<
UITableViewDelegate,
UITableViewDataSource
>
{
UITableView* _tableView;
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* cellStr = @"cell";
UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:cellStr];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
}
NSString* str = [NSString stringWithFormat:@"第%ld组, 第%ld行!", indexPath.section, indexPath.row];
cell.textLabel.text = str;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<
UITableViewDataSource,
UITableViewDelegate
>
{
UITableView* _tableview;
NSMutableArray* _arrayData;
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 320, 536) style:UITableViewStyleGrouped];
_tableview.delegate = self;
_tableview.dataSource = self;
[self.view addSubview:_tableview];
_arrayData = [[NSMutableArray alloc] init];
for (int i = 'A'; i <= 'Z'; i++) {
NSMutableArray* arraySmall = [[NSMutableArray alloc] init];
for (int j = 1; j <= 5; j++) {
NSString* str = [NSString stringWithFormat:@"%c%d", i, j];
[arraySmall addObject:str];
}
[_arrayData addObject:arraySmall];
}
NSLog(@"a = %d", _arrayData.count);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _arrayData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numRow = [[_arrayData objectAtIndex:section] count];
return numRow;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* str = @"cell";
UITableViewCell* cell = [_tableview dequeueReusableCellWithIdentifier:str];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.textLabel.text = _arrayData[indexPath.section][indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"头部标题";
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"尾部标题";
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 60;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<
UITableViewDelegate,
UITableViewDataSource
>
{
UITableView* _tabelView;
NSMutableArray* _arrayData;
UIBarButtonItem* _btnEdit;
UIBarButtonItem* _btnFinsh;
UIBarButtonItem* _btnDelete;
BOOL _isEdit;
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tabelView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tabelView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth;
_tabelView.delegate = self;
_tabelView.dataSource = self;
_tabelView.tableHeaderView = nil;
_tabelView.tableFooterView = nil;
[self.view addSubview:_tabelView];
_arrayData = [[NSMutableArray alloc] init];
for (int i = 1; i < 20; i++) {
NSString* str = [NSString stringWithFormat:@"A %d", i];
[_arrayData addObject:str];
}
[_tabelView reloadData];
[self createBtn];
}
- (void) createBtn
{
_isEdit = NO;
_btnEdit = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(pressEdit)];
_btnFinsh = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(pressFinsh)];
_btnDelete = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(pressDelete)];
self.navigationItem.rightBarButtonItem = _btnEdit;
}
- (void) pressEdit
{
_isEdit = YES;
self.navigationItem.rightBarButtonItem = _btnFinsh;
[_tabelView setEditing:YES];
self.navigationItem.leftBarButtonItem = _btnDelete;
}
- (void) pressFinsh
{
_isEdit = NO;
self.navigationItem.rightBarButtonItem = _btnEdit;
[_tabelView setEditing:NO];
self.navigationItem.leftBarButtonItem = nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _arrayData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* strID = @"ID";
UITableViewCell* cell = [_tabelView dequeueReusableCellWithIdentifier:strID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID];
}
cell.textLabel.text = [_arrayData objectAtIndex:indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tabelView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tabelView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth;
_tabelView.delegate = self;
_tabelView.dataSource = self;
_tabelView.tableHeaderView = nil;
_tabelView.tableFooterView = nil;
[self.view addSubview:_tabelView];
_arrayData = [[NSMutableArray alloc] init];
for (int i = 1; i < 20; i++) {
NSString* str = [NSString stringWithFormat:@"A %d", i];
[_arrayData addObject:str];
}
[_tabelView reloadData];
[self createBtn];
}
- (void) createBtn
{
_isEdit = NO;
_btnEdit = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(pressEdit)];
_btnFinsh = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(pressFinsh)];
_btnDelete = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(pressDelete)];
self.navigationItem.rightBarButtonItem = _btnEdit;
}
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[_arrayData removeObjectAtIndex:indexPath.row];
[_tabelView reloadData];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"选中单元格!%d, %d", indexPath.section, indexPath.row);
}
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"取消选中单元格!%d, %d", indexPath.section, indexPath.row);
}
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void) pressEdit
{
_isEdit = YES;
self.navigationItem.rightBarButtonItem = _btnFinsh;
[_tabelView setEditing:YES];
self.navigationItem.leftBarButtonItem = _btnDelete;
}
- (void) pressFinsh
{
_isEdit = NO;
self.navigationItem.rightBarButtonItem = _btnEdit;
[_tabelView setEditing:NO];
self.navigationItem.leftBarButtonItem = nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _arrayData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* strID = @"ID";
UITableViewCell* cell = [_tabelView dequeueReusableCellWithIdentifier:strID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strID];
}
cell.textLabel.text = [_arrayData objectAtIndex:indexPath.row];
cell.detailTextLabel.text = @"子标题";
NSString* str = [NSString stringWithFormat:@"%d.jpg", indexPath.row % 7 + 1];
UIImage* image = [UIImage imageNamed:str];
cell.imageView.image = image;
return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
心得体会
- UITableViewDelegate,UITableViewDataSource这两个协议必须带上
2.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
这三个函数必须要有
- 数据源一般是个NSMutableArray
- 根据行数找出该有的那个
- 数据的删除要分成两部分,第一步先删除数据源部分,第二步更新数据