
UItableVIEw的强大更多程度上来自于可以任意自定义UItableVIEwCell单元格。通常,UItableVIEw中的Cell是动态的,在使用过程中,会创建一个Cell池,根据每个cell的高度(即tableVIEw:heightForRowAtIndexPath:返回值),以及屏幕高度计算屏幕中可显示几个cell。而进行自定义tableVIEwCell无非是采用代码实现或采用IB编辑nib文件来实现两种方式,本文主要收集代码的方式实现各种cell自定义。
如何动态调整Cell高度
- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath { static Nsstring *CellIDentifIEr = @"Cell"; UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr]; if (cell == nil) { cell = [[[UItableVIEwCell alloc] initWithFrame:CGRectZero reuseIDentifIEr:CellIDentifIEr] autorelease]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; label.tag = 1; label.lineBreakMode = UIlineBreakModeWorDWrap; label.highlightedTextcolor = [UIcolor whitecolor]; label.numberOflines = 0; label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制 label.backgroundcolor = [UIcolor clearcolor]; [cell.contentVIEw addSubvIEw:label]; [label release]; } UILabel *label = (UILabel *)[cell vIEwWithTag:1]; Nsstring *text; text = [textArray objectAtIndex:indexPath.row]; CGRect cellFrame = [cell frame]; cellFrame.origin = CGPointMake(0,0); label.text = text; CGRect rect = CGRectInset(cellFrame,2,2); label.frame = rect; [label sizetoFit]; if (label.frame.size.height > 46) { cellFrame.size.height = 50 + label.frame.size.height - 46; } else { cellFrame.size.height = 50; } [cell setFrame:cellFrame]; return cell;} - (CGfloat)tableVIEw:(UItableVIEw *)tableVIEw heightForRowAtIndexPath:(NSIndexPath *)indexPath{ UItableVIEwCell *cell = [self tableVIEw:tableVIEw cellForRowAtIndexPath:indexPath]; return cell.frame.size.height;} 如何用图片自定义table Separeator分割线
一般地,利用类似[tableVIEw setSeparatorcolor:[UIcolor redcolor]];语句即可修改cell中间分割线的颜色。那又如何用一个图片作为分割线背景呢?可以尝试如下:
方法一:
先设置cell separatorcolor为clear,然后把图片做的分割线添加到自定义的custom cell上。
方法二:
在cell里添加一个像素的imageVIEw后将图片载入进,之后设置tableVIEw.separatorStyle = UItableVIEwCellSeparatorStyleNone
自定义首行Cell与其上面导航栏间距
tableVIEw.tableheaderVIEw = [[[UIVIEw alloc] initWithFrame:CGRectMake(0,0,5,20)] autorelease];
自定义UItableVIEwCell的accessory样式
默认的accessoryType属性有四种取值:UItableVIEwCellAccessoryNone、UItableVIEwCellAccessorydisclosureIndicator、UItableVIEwCellAccessoryDetaildisclosurebutton、UItableVIEwCellAccessorycheckmark。如果想使用自定义附件按钮的其他样式,则需使用UItableVIEw的accessoryVIEw属性来指定。
UIbutton *button;if(isEditableOrNot) { UIImage *image = [UIImage imagenamed:@"delete.png"]; button = [UIbutton buttonWithType:UIbuttonTypeCustom]; CGRect frame = CGRectMake(0.0,0.0,image.size.wIDth,image.size.height); button.frame = frame; [button setBackgroundImage:image forState:UIControlStatenormal]; button.backgroundcolor = [UIcolor clearcolor]; cell.accessoryVIEw = button;}else{ button = [UIbutton buttonWithType:UIbuttonTypeCustom]; button.backgroundcolor = [UIcolor clearcolor]; cell.accessoryVIEw = button;}
以上代码仅仅是定义了附件按钮两种状态下的样式,问题是现在这个自定义附件按钮的事件仍不可用。即事件还无法传递到UItableVIEwDelegate的accessorybuttonTappedForRowWithIndexPath方法上。当我们在上述代码中在加入以下语句:
[button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventtouchUpInsIDe];
后,虽然可以捕捉到每个附件按钮的点击事件,但我们还无法进行区别到底是哪一行的附件按钮发生了点击动作!因为addTarget:方法最多允许传递两个参数:target和event,这两个参数都有各自的用途了(target指向事件委托对象,event指向所发生的事件)。看来只依靠Cocoa框架已经无法做到了。
但我们还是可以利用event参数,在自定义的btnClicked方法中判断出事件发生在UItableVIEw的哪一个cell上。因为UItableVIEw有一个很关键的方法indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个cell的indexPath。而且通过event对象,正好也可以获得每个触摸在视图中的位置。
// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件- (voID)btnClicked:(ID)sender event:(ID)event{ NSSet *touches = [event alltouches]; UItouch *touch = [touches anyObject]; CGPoint currenttouchposition = [touch locationInVIEw:self.tableVIEw]; NSIndexPath *indexPath = [self.tableVIEw indexPathForRowAtPoint:currenttouchposition]; if(indexPath != nil) { [self tableVIEw:self.tableVIEw accessorybuttonTappedForRowWithIndexPath:indexPath]; }}
这样,UItableVIEw的accessorybuttonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。
- (voID)tableVIEw:(UItableVIEw *)tableVIEw accessorybuttonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ int *IDx = indexPath.row; //这里加入自己的逻辑}
http://archive.cnblogs.com/a/2315630/总结
以上是内存溢出为你收集整理的可任意自定义的UITableViewCell全部内容,希望文章能够帮你解决可任意自定义的UITableViewCell所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)