通过以下的几个函数,基本上一个table可以动起来了******************
//最基本的一些用法
//#当Cell的选择是静态的时候用下面的方式初始化 #若为动态生成,那么就使用真实数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [super tableView:tableView numberOfRowsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
//#设置section的数量 cell的数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isOpenArray.count != 0) {
NSInteger count = [isOpenArray[section] intValue];
return count;
}else
{
return 1;
}
}
//#设置cell 这里cell就是一个view 也就是说个人发挥的空间很大 可以做任何想做的事情
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//##这里的Cell是必须写的代码。一开始的时候需要从这里加载
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
return cell;
}
关于table里面的TextField的一些用法******************
//tableView内嵌了scrollView 在文本框与键盘遮挡的情况发生时,TableView会自动的上移来达到不被遮挡的效果,
//但有时候会因为一些个别原因导致这个动作发挥的不是很好,只要是在一下函数中作转换。下面的几个函数是textField的几个代理方法,但中间的元素为table的cell
- (void)textFieldDidBeginEditing:(UITextField*)textField
{
UITableViewCell* cell = [self parentCellFor:textField];
if (cell)
{
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
}
- (void)textFieldDidEndEditing:(UITextField*)textField
{
UITableViewCell* cell = [self parentCellFor:textField];
if (cell)
{
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
[self textFieldDidEndEditing:textField inRowAtIndexPath:indexPath];
//更新tableview的指定section的数据更新
[self.tableView reloadRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationNone];
}
}
//#分析view所在的位置来定位出他的上一级视图
- (UITableViewCell*)parentCellFor:(UIView*)view
{
if (!view)
return nil;
if ([view isMemberOfClass:[UITableViewCell class]])
return (UITableViewCell*)view;
return [self parentCellFor:view.superview];
}
- (void)textFieldDidEndEditing:(UITextField*)textField inRowAtIndexPath:(NSIndexPath*)indexPath;
{
[textField resignFirstResponder];
}
//取消键盘事件
- (void)resignKeyBoardInView:(UIView *)view
{
for (UIView *v in view.subviews) {
if ([v.subviews count] > 0) {
[self resignKeyBoardInView:v];
}
if ([v isKindOfClass:[UITextView class]] || [v isKindOfClass:[UITextField class]]) {
[v resignFirstResponder];
}
}
}
楚楚无敌,转载注明
浙公网安备 33010602011771号