iOS – UItableView 기본 사용 방법 요약

5537 단어

UItableView 기본 사용 방법

  • 우선, Controller는 두 개의 delegate를 실현해야 한다. 그것이 바로 UItable View Delegate와 UItable View DataSource
  • 이다.
  • 그런 다음 UItableView 객체의 delegate를 self로 설정합니다.
  • 그리고 이런 delegate를 실현할 수 있는 몇 가지 방법.

  • (1) 이 방법은tableview에 몇 개의 섹션이 있는지 되돌려줍니다
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 10;
    } 
    

    (2) 이 방법은 대응하는 섹션에 몇 개의 요소, 즉 몇 줄을 되돌려준다.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 10;
    }
    

    (3) 높이
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;//  row  。              
      - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;    //   section header view  。   
      - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;        //   section footer view  。
    

    (4) 지정된 row의 cell을 반환합니다.
    이곳은 비교적 관건적인 곳으로 일반적으로 이곳에서 각종 개성화된cell 요소를 맞춤형으로 제작한다.여기는 가장 간단하고 기본적인cell 타입만 사용합니다.그중에 메인 타이틀cell이 있습니다.textLabel에는 부제목 cell이 있습니다.detail Text Label, 그리고 하나의 이미지는 맨 앞에서cell이라고 합니다.imageView. 오른쪽 아이콘을 설정할 수 있습니다.cell을 통해.Accessory Type은 포만한 오른쪽의 파란색 화살표인지, 얇은 오른쪽 화살표인지, 체크표시인지 설정할 수 있습니다.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
        UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
        if (cell == nil){
            // Create a cell to display an ingredient.
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                           reuseIdentifier:showUserInfoCellIdentifier] 
                    autorelease];
        }
        
        // Configure the cell.
        cell.textLabel.text=@" ";
        cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding];
            }
    

    (5) 지정한 섹션의 헤더 높이를 되돌려줍니다
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        if (section ==0)
            return 80.0f;
        else
            return 30.0f;
    }
    

    (6) 지정한 section 헤더의 타이틀을 되돌려줍니다. 만약 이 section 헤더가view를 되돌려준다면 타이틀은 작동하지 않습니다
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        if (tableView == tableView_){
            if (section == 0) {
                return @"title 1";
            } 
            else if (section == 1) {
                return @"title 2";
            } 
            else {
                return nil;
            }
        } 
        else{
            return nil;
        }
    }
    

    (7) 지정한 섹션 헤더의view를 되돌려줍니다. 없으면 이 함수는view를 되돌려주지 않을 수 있습니다
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        if (section == 0) { 
            UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView" 
                                                            owner: self
                                                          options: nil] lastObject];
         
            else {
               return nil;
            }
    }
    

    (8) select 클릭 함수에 응답하고 어느 section, 어느 row에 따라 스스로 응답하면 된다.
    사용자가 어떤 줄의cell을 선택했을 때 이것을 리셋합니다.그러나 우선tableview의 속성을 select로 설정해야 합니다.TableView.allowsSelection=YES; cell.selectionStyle=UITableViewCellSelectionStyleBlue; select에 응답하지 않으려면 다음 코드로 속성을 설정할 수 있습니다: TableView.allowsSelection=NO; 셀이 셀렉트에 응답할 수 있도록 어떻게 해야 합니까? 선택한 색상이 바뀌지 않도록 셀을 설정합니다.selectionStyle = UITableViewCellSelectionStyleNone; 응답 select 클릭 함수
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section == 1) {
            return;
        }
        else if(indexPath.section==0){
            switch (indexPath.row) {
                // 
                case 0:{
                    [self  onTalkToFriendBtn];
                }
                    break;
                default:
                    break;
            }
        }
        else {
            return ;
        }
    }
    

    (9)tableview 줄마다 분할선을 설정하는 방법self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine; 분할선이 필요하지 않으면 UItable ViewCell Separator StyleNone으로 속성을 설정하면 됩니다.
    (10) tableview cell의 배경색을 설정하는 방법
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
            // 
            cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];
    }
    

    (11) 이 함수 응답은 사용자가cell 오른쪽에 있는 화살표를 클릭합니다 (있으면)
    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath    
    

    (12)tableview를 편집할 수 있도록 설정하는 방법
    먼저 편집 모드에 들어갑니다. [TableView setEditing: YES animated: YES];편집 모드를 종료하려면 NO로 설정해야 합니다.
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
              return UITableViewCellEditingStyleDelete;// 
    }
    -(void) tableView:(UITableView *)aTableView
    commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
    forRowAtIndexPath:(NSIndexPath *)indexPath{
            [chatArray  removeObjectAtIndex:indexPath.row];
            [chatTableView  reloadData];
    }
    

    (13) 행 CELL 객체 획득 방법- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

    좋은 웹페이지 즐겨찾기