TableView详细解释

 -、建立 UITableView

 DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
 [DataTable setDelegate:self];
 [DataTable setDataSource:self];
 [self.view addSubview:DataTable];
 [DataTable release];
 
二、UITableView各Method说明
 
//Section总数
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
 return TitleData;
}
 
// Section Titles
//每个section显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
 return @"";
}
 
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 return 4;
}
 
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
 
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             SimpleTableIdentifier];
    if (cell == nil) {  
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier: SimpleTableIdentifier] autorelease];
 }
 cell.imageView.image=image;//未选cell时的图片
 cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
 cell.text=//.....
 return cell;
}
 
//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
 NSUInteger row = [indexPath row];
 return row;
}
 
//改变行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
 
//定位
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];
 
//返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
 
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
 
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}
 
//判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    if (row == 0)
        return nil;
   
    return indexPath;
}
 
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
}
 
//编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
}
//返回Section标题内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}
//自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
三、在UITableViewCell上建立UILable多行显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
  UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
  [Datalabel setTag:100];
  Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  [cell.contentView addSubview:Datalabel];
  [Datalabel release];
 } 
 UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];
 [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];
 Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];
 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
//选中cell时的颜色
typedef enum {
    UITableViewCellSelectionStyleNone,
    UITableViewCellSelectionStyleBlue,
    UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle 
//cell右边按钮格式
typedef enum {
    UITableViewCellAccessoryNone,                   // don't show any accessory view
    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
    UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
    UITableViewCellAccessoryCheckmark               // checkmark. doesn't track
} UITableViewCellAccessoryType
//是否加换行线
typedef enum {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle//改变换行线颜色
tableView.separatorColor = [UIColor blueColor];
 

使用空白view取代cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  //取消选中颜色

  UIView *backView = [[UIView alloc] initWithFrame:cell.frame];
  cell.selectedBackgroundView = backView;
  cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];

  //取消边框线

  [cell setBackgroundView:[[UIView alloc] init]];          //取消边框线
      cell.backgroundColor = [UIColor clearColor];

 

//在navigation中tableviewCell选中后返回无选中项

//单击一个cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 
    if(cell.tag == 0){

   //注销cell单击事件
        cell.selected = NO;
    }else {
        [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];  //取消选中项
        BabyScheduler *babyScheduler=[listData objectAtIndex:indexPath.row-1];
        [delegate showVaccinationView:babyScheduler];    
    }
}

- (void)viewDidLoad
{
    self.title = NSLocalizedString(@"TempGroupViewTitle", @"");
    self.view.backgroundColor=[UIUtils defaultViewBackground];
    self.tempGroupTableView.backgroundColor=[UIColor clearColor];
    self.tempGroupTableView.separatorColor=[UIColor clearColor];    //分割cell线颜色
    self.tempGroupTableView.separatorStyle=UITableViewCellSeparatorStyleNone;   //不带分割线样式
    self.tempGroupTableView.rowHeight=45.0;
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;   //添加navigation按钮
    self.groupList = [DBManager selectTempGroup];   //获取分组信息
//    NSLog(@"-----%d",[groupList count]);
    [super viewDidLoad];
}

if (!cell)----当cell为空?真:假

//设置cell的高度

#pragma mark - Table view delegate
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section==1)return 45;
    return 0;
}

//返回自定义hrader
-(UIView*) tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==1) { //第二区
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 305, 38)];
        UIImageView* backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title.png"]];
        backgroundView.frame=CGRectMake(0, 0, 123, 38);
        [view addSubview:backgroundView];
        [backgroundView release];
        view.backgroundColor=[UIColor clearColor];
        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(30, 0, 93, 38)];
        label.backgroundColor=[UIColor clearColor];       
        label.textColor=[UIColor whiteColor];
        label.text=NSLocalizedString(@"Section_Title_My_Group_Name", @"");
        [view addSubview:label];
        [label autorelease];
        return  [view autorelease];
    }
    return nil;
}

//向tableview填充数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当第一个分区得最后一行
    if ((indexPath.section==0)&&(indexPath.row==[groupList count])) {
        static NSString *AddGroupViewCellIdentifier = @"AddGroupViewCell";
        
        UITableViewCell *cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AddGroupViewCellIdentifier] autorelease];
        // key 说明性文字
        cell.textLabel.text=NSLocalizedString(@"Add_New_Group", @"add new group");
        cell.textLabel.backgroundColor=[UIColor clearColor];
        cell.textLabel.textAlignment=UITextAlignmentCenter;     //cell中text文本居中
        cell.backgroundColor=[UIUtils defaultContactCellBackgroundColor];
        cell.tag=-1;
        return  cell;
    }
    static NSString *SimpleTableIdentifier = @"GroupListViewCell";
    //使用自定义cell
    //查找SimpleTableIdentifier的cell,为空初始化
    GroupListViewCell *cell = (GroupListViewCell *)[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (!cell)
    {
        [[NSBundle mainBundle] loadNibNamed:SimpleTableIdentifier owner:self options:nil];
        cell = groupCell;
        cell.backgroundColor=[UIUtils defaultContactCellBackgroundColor];
        self.groupCell = nil;        
    }
    cell.group=[groupList objectAtIndex:indexPath.row];

//设置cell右边箭头,v等等,有枚举变量可供选择
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSUInteger row = [indexPath row];
    cell.tag = row;
    [SimpleTableIdentifier release];
    return cell;
    
}

 


cell可删除

// 指定tableview可删除的区域
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.section==1?YES:NO;
}
//可删除的cell
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView
          editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUInteger row = [indexPath row];
    if (row == [groups count]) {
        return UITableViewCellEditingStyleNone;
    }else {
        return UITableViewCellEditingStyleDelete;
    }
}

// 删除之后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [self deleteGroup:indexPath.row];
        [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
    }   
}

 

//当前选中行设为非选中

[self.membersListView deselectRowAtIndexPath:membersListView.indexPathForSelectedRow animated:YES];

你可能感兴趣的:(tableview)