本文共 8262 字,大约阅读时间需要 27 分钟。
1.UITableView有两种样式:
- [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
- [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStyleGrouped];
2.UITableView的结构:
UITableView由头部,尾部,和中间一连串的单元格组成,UITableView的头部由tableHeaderView属性设置,尾部由tableFooterView属性设置,中间的
行高可通过rowHeight属性设置
- _listArray = [[UIFont familyNames] retain];
-
- _tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
-
- _tableView.dataSource = self;
-
- _tableView.delegate = self;
-
- _tableView.rowHeight = 70;
-
- UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMG_0410"]];
- _tableView.backgroundView = backgroundView;
- [backgroundView release];
-
- _tableView.backgroundColor = [UIColor yellowColor];
-
- _tableView.separatorColor = [UIColor purpleColor];
-
- _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
-
- UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
- headerView.backgroundColor = [UIColor redColor];
-
- UILabel *headText = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 200, 80)];
- headText.text = @"天晴朗,天晴朗天晴朗天晴朗!";
- headText.numberOfLines = 0;
- [headerView addSubview:headText];
- [headText release];
- _tableView.tableHeaderView = headerView;
- [headerView release];
-
- UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
- footerView.backgroundColor = [UIColor yellowColor];
- _tableView.tableFooterView = footerView;
- [footerView release];
UITableView的一些常用属性
-
- @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;
-
- @property(nonatomic,retain) UIColor *separatorColor;
-
- @property(nonatomic,retain) UIView *tableHeaderView;
-
- @property(nonatomic,retain) UIView *tableFooterView;
-
- @property (nonatomic) CGFloat rowHeight;
-
- @property (nonatomic) CGFloat sectionHeaderHeight;
-
- @property (nonatomic) CGFloat sectionFooterHeight;
-
- @property(nonatomic, readwrite, retain) UIView *backgroundView NS_AVAILABLE_IOS(3_2);
-
- @property(nonatomic,getter=isEditing) BOOL editing;
- - (void)setEditing:(BOOL)editing animated:(BOOL)animated;
-
- @property(nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);
-
- @property(nonatomic) BOOL allowsSelectionDuringEditing;
-
- @property(nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0);
-
- @property(nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);
UITableView的一些常用方法:
-
- - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
-
- - (NSArray *)indexPathsForRowsInRect:(CGRect)rect;
-
- - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
-
- - (NSArray *)visibleCells;
-
- - (NSArray *)indexPathsForVisibleRows;
UITableView的一些编辑方法: -
- - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
-
- - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
-
- - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
-
- - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);
UITableView数据源方法
-
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return 1;
- }
-
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- {
- return [_listArray count];
- }
-
-
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *cellIdentifier = @"cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
-
-
-
-
-
-
- }
-
-
- cell.textLabel.textColor = [UIColor redColor];
- cell.textLabel.font = [UIFont fontWithName:fontName size:18];
- cell.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
- cell.detailTextLabel = @"detailTextLabel"
- return cell;
-
- }
-
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
-
- - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
-
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
-
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
-
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
-
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
-
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
- {
- return _keyArray;
- }
-
- - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
- {
- return index;
- }
UITalbeView常用的代理方法
-
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
-
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
-
- - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
-
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
-
- - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
-
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
-
- - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
UITableViewCell的一些辅助功能
//sell的选中样式
- cell.selectionStyle = UITableViewCellSelectionStyleBlue;
如果想选中后取消,在didSelectRowAtIndexPath方法中调用
- [tableView deselectRowAtIndexPath:indexPath animated:YES];或
- [self performSelector:@selector(deselectRowAtIndexPath:animated:) withObject:indexPath afterDelay:.5];
如果想在cell的右边出现选中状态或箭头可以设置下面的属性
- cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell根据文字的多少自适应高度
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
-
- NSString *text = [_listArray objectAtIndex:indexPath.row];
-
- CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(320, 1000)];
-
- return size.height+20;
- }
-
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
- [tableViewsetSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
转载地址:http://ylzfa.baihongyu.com/