![](/img/trans.png)
[英]UITableView selected row center like UIPickerView in Swift
[英]how to develop UITableView which work like UIPickerView and selected row height is big then other row's in iOS?
我已經在UIViewController
創建了一個UITableView
在所有相同的行UIViewController
工作,但是我想增加所選行的行高 。 我想在滾動后在中間選擇時在紫pur顏色單元格中設置更多的行高。 我的代碼和輸出如下所示:
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
終於得到了正確的解決方案...! 聽說我需要在scrollViewDidScroll中編寫代碼:用於獲取scrollview的內容偏移量的方法,用於控制所選行的位置。 此邏輯代碼與scrollViewWillEndDragging:方法相同,只是設置了全局變量,並最后寫入了scrollViewDidScroll方法而不是調用函數。
if (testRounded<=0)
{
Value = 0;
}
else if (testRounded>=29)
{
Value = 29;
}
else
{
Value = testRounded;
}
然后在編寫代碼后添加刷新表,並設置更多的選定行高。 :
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"value: %ld",(long)Value);
RowAtIndex = Value;
CGPoint offset = CGPointMake(0, ((RowAtIndex*44)));
[scr setContentOffset:offset animated:YES];
[tbl reloadData];
}
然后設置高度 :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == RowAtIndex)
{
return 64;
}
else
{
return 44;
}
}
並在cellForRowAtIndexPath中更改所選單元格的顏色:
if (RowAtIndex == indexPath.row)
{
cell.backgroundColor = [UIColor purpleColor];
}
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.