带我飞5iOS 开发 iOS Developer

tableView你不知道的小知识

2016-05-13  本文已影响300人  gao_smile

//------选中某行让其置顶
- (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;
}

- (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];
  }
}
上一篇 下一篇

猜你喜欢

热点阅读