第一次在简书上写文章
2016-11-18 本文已影响204人
码农冰冰


代码实现:
- cellForRowAtIndexPath方法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(@"11111111111111");
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor =[UIColor clearColor];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
selectedBackgroundView.backgroundColor = [UIColor whiteColor];
cell.selectedBackgroundView = selectedBackgroundView;
// 左侧示意条
UIView *liner = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 2, 40)];
liner.backgroundColor = [UIColor orangeColor];
[selectedBackgroundView addSubview:liner];
}
cell.textLabel.text=[NSString stringWithFormat:@"第%ld个",indexPath.row];
return cell;
}
- 代理方法:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"22222222222222");
if (_isRelate==YES) {
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0){
NSLog(@"33333333333333");
_isRelate=NO;
}
当然也有很多其他的实现方式就不多做介绍了。