tableView你不知道的小知識

//------選中某行讓其置頂
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[firstTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

}


//--------初始化默認讓其選中某行
 NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];

[firstTable selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

//-----通過數據的值獲取數組的下標
NSInteger line = 0;

    if ([setArray containsObject:section]) {

       line = [setArray indexOfObject:section];
    }


#pragma mark - 實現Table View協議
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45;
}
  • tableView如何顯示數據
    • 設置dataSource數據源
    • 數據源要遵守UITableViewDataSource協議
    • 數據源要實現協議中的某些方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == firstTable) {
    return departmentsDict.count;
} else {
    return  secondSaveArray.count;
     }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *identifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}

if (tableView == firstTable)
{
    UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, WMDeviceWidth * 0.38 - 20, 44)];
    nameLable.backgroundColor = [UIColor clearColor];
    nameLable.text = [firstSaveArray objectAtIndex:indexPath.row];
    nameLable.font = [UIFont boldSystemFontOfSize:16];
    nameLable.textColor = [ColorFromHexRBG colorFromHexRGB:@"5F5F5F"];
    [cell addSubview:nameLable];

    cell.backgroundColor = [ColorFromHexRBG colorFromHexRGB:@"EDECEC"];

    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
}
else if (tableView == secondTable)
{
    UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, WMDeviceWidth * 0.62 - 20, 44)];
    nameLable.backgroundColor = [UIColor clearColor];
    nameLable.text = [[secondSaveArray objectAtIndex:indexPath.row] objectForKey:@"deptName"];
    nameLable.font = [UIFont boldSystemFontOfSize:16];
    nameLable.textColor = [ColorFromHexRBG colorFromHexRGB:@"5F5F5F"];
    [cell addSubview:nameLable];

    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(10, 44, WMDeviceWidth * 0.62 - 18, 1)];
    line.backgroundColor = [ColorFromHexRBG colorFromHexRGB:@"C4C4C4"];
    [cell addSubview:line];

    UIImageView *inputImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WMDeviceWidth * 0.62 - 35, 10, 30, 25)];
    inputImageView.image = [UIImage imageNamed:@"進入"];
    [cell addSubview:inputImageView];
 }

return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == firstTable)
{
   // [firstTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];

    secondSaveArray = [departmentsDict objectForKey:[firstSaveArray objectAtIndex:indexPath.row]];
    myInteger=indexPath.row;
    [firstTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    [secondTable reloadData];
    }
    else if (tableView == secondTable)
    {
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    [userDefaults setObject:[firstSaveArray objectAtIndex:myInteger] forKey:@"firstDeptName"];
    NSString *s11 = [userDefaults objectForKey:@"firstDeptName"];
    WMDoctorViewController *Doctor = [[WMDoctorViewController alloc]init];



    //傳到下一頁面
    Doctor.hospitalName = hospitalName;
    Doctor.hospitalId = hospitalId;
    Doctor.departmentID = [[secondSaveArray objectAtIndex:indexPath.row] objectForKey:@"importDeptId"];
    Doctor.departmentDetail = [[secondSaveArray objectAtIndex:indexPath.row] objectForKey:@"deptName"];

    //保存到userDefaults中
    [userDefaults setObject:hospitalName forKey:@"hospitalName"];
    [userDefaults setObject:hospitalId forKey:@"hospitalId"];
    [userDefaults setObject:Doctor.departmentDetail forKey:@"deptName"];
    NSString *s = [userDefaults objectForKey:@"deptName"];
    [userDefaults setObject:Doctor.departmentID forKey:@"departmentID"];

    [userDefaults synchronize];

    [self.navigationController pushViewController: Doctor animated:YES];
  }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • { 24、Sqlite數據庫 1、存儲大數據量,增刪改查,常見管理系統:Oracle、MSSQLServer、DB...
    CYC666閱讀 959評論 0 1
  • 一、簡介 <<UITableView(或簡單地說,表視圖)的一個實例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無邪8閱讀 10,663評論 3 3
  • #pragma mark someValueAboutTableView 1.tableView的樣式:UITab...
    瀟巖閱讀 943評論 0 0
  • 1.概念 它是基于UIScrollView的列表互動類。使用UITableView可以在屏幕上顯示單元格的列表,每...
    棲息于曠野閱讀 1,140評論 0 0
  • IOS中UITableView使用總結 一、初始化方法 - (instancetype)initWithFrame...
    脫下國際籃化身程序猿閱讀 768評論 0 2