UItableView의 일반적인 방법 및 속성

3139 단어

tableView에서 데이터를 보여주는 방법

  • 데이터 소스 객체 설정
  • self.tableView.dataSource = self;
    
  • 데이터 원본 대상은 협의를 준수해야 한다
  • @interface ViewController () 
    
    @end
    
  • 데이터 소스 구현 방법
  • //  
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
    
    //  
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    
    //  
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    
    //  
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    
    //  
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    

    tableView의 일반 설정

    //  cell 
    self.tableView.rowHeight = 100;
    
    //  
    self.tableView.sectionHeaderHeight = 50;
    
    //  
    self.tableView.sectionFooterHeight = 50;
    
    //  
    self.tableView.separatorColor = [UIColor redColor];
    //  
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //  
    self.tableView.tableHeaderView = [[UISwitch alloc] init];
    //  
    self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    
    //  
    self.tableView.sectionIndexColor = [UIColor redColor];
    //  
    self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];
    

    tableViewCell의 일반 설정

    //  
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //  
    cell.accessoryView = [[UISwitch alloc] init];
    
    //  cell 
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // backgroundView  > backgroundColor
    
    //  
    cell.backgroundColor = [UIColor redColor];
    
    //  view
    UIView *bg = [[UIView alloc] init];
    bg.backgroundColor = [UIColor blueColor];
    cell.backgroundView = bg;
    
    //  view
    UIView *selectedBg = [[UIView alloc] init];
    selectedBg.backgroundColor = [UIColor purpleColor];
    cell.selectedBackgroundView = selectedBg;
    

    cell의 순환 이용

  • 전통적인 쓰기
  • /**
     *   cell , 
     */
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID = @"wine";
    
        // 1. cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        // 2. cell
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
    
        // 3. 
        cell.textLabel.text = [NSString stringWithFormat:@"%zd ", indexPath.row];
    
        return cell;
    }
    
  • 새로운 쓰기 (등록cell)
  • NSString *ID = @"wine";
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //     Cell 
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 1. cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        // 2. 
        cell.textLabel.text = [NSString stringWithFormat:@"%zd ", indexPath.row];
    
        return cell;
    }
    

    좋은 웹페이지 즐겨찾기