demo下载地址:http://download.csdn.net/detail/robinson_911/8350827
1.滑动删除UITableView的某一列,用到以下3个方法
//删除用的三个方法
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 显示删除改行时的“删除”二字。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 是否可以删除,返回YES的话,表示该行可以被删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 删除提交,同事刷新数据源。
下面是效果图:
2. UITableView 代理方法 部分源代码展示
#pragma mark ------ UITableViewDataSource
//表示有多少个Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
//每个中得每一条Item数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myArray count];
}
//每一个具体Item的cell的风格及其数据源的加载
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* static NSString *TableViewDynamicLoadIdentifier = @"SwwTableViewCell";
SwwTableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:TableViewDynamicLoadIdentifier];
if (cells == nil)
{
cells = [[[SwwTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:TableViewDynamicLoadIdentifier]autorelease] ;
cells.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSInteger nRow = [indexPath row];
// SwwTableViewCell *shopdata = [self.myArray objectAtIndex:nRow];
// cells.label1.text = shopdata.text1;
// cells.label2.text = shopdata.text2;
// cells.label3.text = shopdata.text3;
// cells.label4.text = shopdata.text4;
// cells.label5.text = shopdata.text5;
cells.label1.text = [self.myArray objectAtIndex:0];
cells.label2.text = [self.myArray objectAtIndex:1];
cells.label3.text = [self.myArray objectAtIndex:2];
cells.label4.text = [self.myArray objectAtIndex:3];
cells.label5.text = [self.myArray objectAtIndex:4];
return cells;
*/
static NSString *TableViewDynamicLoadIdentifier = @"Cell";
UITableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:TableViewDynamicLoadIdentifier];
if (cells == nil)
{
cells = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:TableViewDynamicLoadIdentifier]autorelease] ;
cells.selectionStyle = UITableViewCellSelectionStyleNone;
}
//将单元格边角设置为圆角
cells.layer.cornerRadius = 12;
cells.layer.masksToBounds = YES;
//从IndexPath参数中获取当前的行号
NSInteger rowNo = indexPath.row;
//取出每一个cell的标题
cells.textLabel.text = [self.myArray objectAtIndex:rowNo];
//为cell得左端设置图片
cells.imageView.image = [UIImage imageNamed:@"TitleView"];
//取出details中索引为rowNo的元素作为cell的详细内容
cells.detailTextLabel.text = [self.myArray objectAtIndex:rowNo];
return cells;
}
//每一个cell的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60;
}
//每一个section中头部标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ( section == 0 )
{
return 30.0;
}else if (section == 1){
return 80.0;
}
return 0.0
}
//删除用的三个方法 某一行能否被删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//删除用的三个方法 具体的删除某一条Item
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.myArray removeObjectAtIndex:indexPath.row];
// Delete the row from the data source.
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
#pragma mark ------ UITableViewDelegate
//每一个section中头部标题的具体内容定义
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headView = [[[UIView alloc] init] autorelease];
[headView setFrame:CGRectMake(0, 100, 320, 30)];
headView.backgroundColor = [UIColor whiteColor];
// headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//边框2
UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 300, 1)];
borderView.layer.backgroundColor = [[UIColor grayColor] CGColor];
borderView.layer.borderWidth = 1.0f;
[headView addSubview:borderView];
[borderView release];
UILabel *_orderLable = [self _generateHintLabel];
[_orderLable setFrame:CGRectMake(3, headerHight, 60, 20)];
[headView addSubview:_orderLable];
UILabel *_productLable = [self _generateHintLabel];
[_productLable setFrame:CGRectMake(60, headerHight, 60, 20)];
[headView addSubview:_productLable];
UILabel *_doLable = [self _generateHintLabel];
[_doLable setFrame:CGRectMake(125, headerHight, 60, 20)];
[headView addSubview:_doLable];
UILabel *_buyTimeLable = [self _generateHintLabel];
[_buyTimeLable setFrame:CGRectMake(165, headerHight, 60, 20)];
[headView addSubview:_buyTimeLable];
UILabel *_buyMoneyLable = [self _generateHintLabel];
[_buyMoneyLable setFrame:CGRectMake(240, headerHight, 60, 20)];
[headView addSubview:_buyMoneyLable];
//第一个section的标题栏
if ( section == 0 )
{
_orderLable.text = @"订单号";
_productLable.text = @"产品名称";
_doLable.text = @"操作";
_buyTimeLable.text = @"购买时间";
_buyMoneyLable.text = @"购买金额";
}//第二个section的标题栏
else if ( section == 1 )
{
_orderLable.text = @"订单号";
_productLable.text = @"产品";
_doLable.text = @"操作";
_buyTimeLable.text = @"购买";
_buyMoneyLable.text = @"金额";
}
return headView;
}
//删除用的三个方法 显示删除时右边的按钮
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *titileString = [NSString stringWithFormat:@"你点击了按钮%ld",(long)[indexPath row]];
UIAlertView *alert = [[[ UIAlertView alloc]initWithTitle:@"提示" message:titileString delegate:nil cancelButtonTitle:@"OK"otherButtonTitles: nil]autorelease];
[alert show];
NSInteger rownum = [indexPath row];
NSLog(@"===row=%ld",(long)rownum);
}
- (UILabel *)_generateHintLabel
{
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.font = [UIFont boldSystemFontOfSize: 13.0f];
tempLabel.backgroundColor = [UIColor clearColor];
return [tempLabel autorelease];
}
3.充分用好IOS本身的属性
默认的UITableViewCell中包含三个可以配置的属性
textLable :代表cell的标题
detailTextLabel :代表cell显示的详细内容
image: 代表cell左边得图标
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *TableViewDynamicLoadIdentifier = @"Cell";UITableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:TableViewDynamicLoadIdentifier];if (cells == nil){cells = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:TableViewDynamicLoadIdentifier]autorelease] ;cells.selectionStyle = UITableViewCellSelectionStyleNone;}//将单元格边角设置为圆角cells.layer.cornerRadius = 12;cells.layer.masksToBounds = YES;//从IndexPath参数中获取当前的行号NSInteger rowNo = indexPath.row;//取出每一个cell的标题cells.textLabel.text = [self.myArray objectAtIndex:rowNo];//为cell得左端设置图片cells.imageView.image = [UIImage imageNamed:@"TitleView"];//取出details中索引为rowNo的元素作为cell的详细内容cells.detailTextLabel.text = [self.myArray objectAtIndex:rowNo];return cells;
}