I have created one UITableView
in UIViewController
which work's with all same row height but I want to increase the row height of selected row than other row's. I want to set row height more which is in purpole color cell when it get selected in middle after scrolling. My code and output is as bellow:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UITableView *tbl;
IBOutlet UIScrollView *scr;
int pos;
}
@end
ViewController.m
#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated
{
scr.frame = CGRectMake(15, 0, 280, 480);
scr.contentSize = CGSizeMake(scr.frame.size.width, 1320 + 480 - 44);
tbl.frame = CGRectMake(15, 218, 280, 1320);
}
#pragma mark - TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"Cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =[NSString stringWithFormat:@"Hello row no. %ld",(long)[indexPath row]];
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
-(void)setscroll:(int)value
{
[tbl reloadData];
NSUInteger indexArr[] = {0,value};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:2];
TableViewCell * theCell = (TableViewCell *)[tbl cellForRowAtIndexPath:indexPath];
theCell.backgroundColor = [UIColor purpleColor];
[tbl setContentOffset:CGPointMake(0,theCell.center.y-((44*value)+22)) animated:NO];
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
CGFloat rowHeight = 44;
NSLog(@"targetContentOffset %f",targetContentOffset->y);
CGFloat floatVal = targetContentOffset->y / rowHeight;
NSInteger rounded = (NSInteger)(lround(targetContentOffset->y));
NSInteger testRounded = (NSInteger)(lround(floatVal));
NSLog(@"rounded %ld",(long)rounded);
NSLog(@"testRounded %ld",(long)testRounded);
if (testRounded<=0)
{
[self setscroll:0];
}
else if (testRounded<=29)
{
[self setscroll:29];
}
else
{
[self setscroll:testRounded];
}
}
@end
Finally Got the correct Solution...! hear i have need to write code in scrollViewDidScroll: method for get content offset of scrollview for gate the position of selected row. this logic code same as scrollViewWillEndDragging: method just set global variable and write in scrollViewDidScroll method last instead of call function.
if (testRounded<=0)
{
Value = 0;
}
else if (testRounded>=29)
{
Value = 29;
}
else
{
Value = testRounded;
}
then after write add code for refresh table and set selected row height more. :
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"value: %ld",(long)Value);
RowAtIndex = Value;
CGPoint offset = CGPointMake(0, ((RowAtIndex*44)));
[scr setContentOffset:offset animated:YES];
[tbl reloadData];
}
and then after set the height :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == RowAtIndex)
{
return 64;
}
else
{
return 44;
}
}
and in cellForRowAtIndexPath for change the selected cell color:
if (RowAtIndex == indexPath.row)
{
cell.backgroundColor = [UIColor purpleColor];
}
Output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.