
当我们点击某个cell时,会执行下面这个方法,方法中调用另一方法执行具体 *** 作:
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath {
if (0 == indexPathsection) {
[self btnActionForUserSetting:self];
}
}
在下面方法中怎样获取刚刚选中的那个cell,并修改它的内容呢?
- (void)btnActionForUserSetting:(id) sender {
NSIndexPath indexPath = [selftableView indexPathForSelectedRow];
UITableViewCell cell = [selftableView cellForRowAtIndexPath:indexPath];
celltextLabeltext= @"abc";
}
这样,被点击的cell的内容就被修改了。
假如你是用代码方式直接将控件(如UILabel、UIButton等)加到UITableView的cell中去的话,,,在出了
[cpp] view plaincopyprint
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath
{
//自定义代码
return cell;
}
这个函数后,,,当你点击cell的时候想知道到底是点击了第几行,,这时候你就可以通过在以下代码获得点击的行数。
[cpp] view plaincopyprint
UITableViewCell cell = (UITableViewCell )[btn superview];
NSIndexPath indexPath = [_myTableView indexPathForCell:cell];
NSLog(@"indexPath is = %i",indexPathrow);
注释:btn是你通过代码的方式添加到cell的一个Button,_myTableView是UITableView的一个关联变量。
假如你是通过新建 xib的方式新建一个继承UITableViewCell的 xib(例如:shopCellxib)文件添加到原有UITableView的cell的方式的话,,,用上面这种方法是获取不到点击cell所在的行数的,也就是说你不知道点击的cell到底是第几行。
同样可以用上面的代码,,不过要稍微修改一下:
[cpp] view plaincopyprint
UITableViewCell cell = (UITableViewCell )[[[btn superview] superview] superview];
NSIndexPath indexPath = [_myTableView indexPathForCell:cell];
NSLog(@"indexPath is = %i",indexPathrow);
解释:第一句代码中的[btn superview]是shopCell 的contentView,第二个superview是shopCell自己本身的cell,第三个superview是UITableView的cell,,注意不要弄混淆了。
20131024再次编辑:
知道row,然后通过row确定是哪一行的cell
[cpp] view plaincopyprint
NSIndexPath index1 = [NSIndexPath indexPathForItem:row inSection:0];
UITableViewCell cell1 = [_myTableView cellForRowAtIndexPath:index1];
一般的话- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath 这个方法里应该都会设置cell内的显示内容,在didselect里面你把上面那个方法里的[xxxx objectAtIndex:indexPathrow] xxxxx]再次赋给你自己要取指的对象不就可以了么
ell本身不需要你去删除。你是想先标记下行,最后删除编辑的行。这实现起来有很多方法。你的行数是怎么控制的是关键。你说的太笼统了,如果表的行数是动态控制的直接删除数据源中相应的数据就能删除行,这样你需要记录标记的cell的indexpath,可以放在数组里,然后再用table的代理方法把cell delete掉。
iOS SkeletonView
SkeletonView 一种优雅的方式,向用户显示正在发生的事情,并为他们正在等待的内容做好准备
1安装
pod "SkeletonView"
2如何使用
21在适当的位置导入SkeletonView
import SkeletonView
22设置视图skeletonables
avatarImageViewisSkeletonable = true
23显示骨架
viewshowSkeleton()// SolidviewshowGradientSkeleton()// GradientviewshowAnimatedSkeleton()// Solid animatedviewshowAnimatedGradientSkeleton()// Gradient animated
24更新骨架(如颜色,动画等)
viewupdateSkeleton()// SolidviewupdateGradientSkeleton()// GradientviewupdateAnimatedSkeleton()// Solid animatedviewupdateAnimatedGradientSkeleton()// Gradient animated
25隐藏骨架
viewhideSkeleton()
3UITableView
tableViewjpg
31遵守SkeletonTableViewDataSource协议,并实现协议方法
extensionSearchMovieViewController:SkeletonTableViewDataSource{functableView(_tableView:UITableView,numberOfRowsInSection section:Int)->Int{returnselfmodelscount}functableView(_tableView:UITableView,cellForRowAt indexPath:IndexPath)->UITableViewCell{letmodel=selfmodels[indexPathrow]letcell=tableViewdequeueReusableCell(withIdentifier:"SearchMovieCell",for:indexPath)as!SearchMovieCellifmodelvod_picisEmpty==false{cellmovieImageViewkfindicatorType=activity cellmovieImageViewkfsetImage(with:ImageResource(downloadURL:URL(string:modelvod_pic!)!),placeholder:UIImage(named:"zanwutupian"))}celltitleLabeltext=modelvod_name celldetailsLabeltext=modelvod_blurbreturncell}funccollectionSkeletonView(_skeletonView:UITableView,cellIdentifierForRowAt indexPath:IndexPath)->ReusableCellIdentifier{return"SearchMovieCell"}}
32 设置视图skeletonables
classSearchMovieCell:DiaryBaseTableViewCell{/// varmovieImageView:UIImageView!/// 标题vartitleLabel:UILabel!/// 详情vardetailsLabel:LCZAlignTopLabel!overridefuncconfig(){selfisSkeletonable=truemovieImageView=UIImageView()selfcontentViewaddSubview(movieImageView)movieImageViewsnpmakeConstraints{(make)inmakeleftequalToSuperview()offset(15)makecenterYequalToSuperview()makeheightequalTo(100)makewidthequalTo(80)}movieImageViewlayercornerRadius=5movieImageViewclipsToBounds=truemovieImageViewcontentMode=scaleAspectFill movieImageViewsetContentHuggingPriority(required,for:horizontal)movieImageViewsetContentCompressionResistancePriority(required,for:horizontal)movieImageViewisSkeletonable=truetitleLabel=UILabel()selfcontentViewaddSubview(titleLabel)titleLabelsnpmakeConstraints{(make)inmakeleftequalTo(movieImageViewsnpright)offset(5)maketopequalTo(movieImageViewsnptop)makerightequalToSuperview()offset(-15)}titleLabelfont=LCZBoldFontSize(size:16)titleLabelsetContentHuggingPriority(required,for:vertical)titleLabelsetContentCompressionResistancePriority(required,for:vertical)titleLabeltext="xxxxxxxxx"titleLabelisSkeletonable=truetitleLabeltextAlignment=leftdetailsLabel=LCZAlignTopLabel()selfcontentViewaddSubview(detailsLabel)detailsLabelsnpmakeConstraints{(make)inmakeleftequalTo(movieImageViewsnpright)offset(5)maketopequalTo(titleLabelsnpbottom)offset(5)makerightequalToSuperview()offset(-15)makebottomequalTo(movieImageViewsnpbottom)}detailsLabelfont=LCZFontSize(size:14)detailsLabelnumberOfLines=0;detailsLabeltextColor=LCZRgbColor(160,160,160,1)detailsLabelisSkeletonable=truedetailsLabeltextAlignment=left}}
33 显示骨架
selfsearchMovieViewtableViewvisibleCellsforEach{$0showAnimatedGradientSkeleton(usingGradient:SkeletonGradient(baseColor:UIColorclouds),animation:GradientDirectiontopLeftBottomRightslidingAnimation())}
4UICollectionView
collectionViewjpg
41 遵守SkeletonCollectionViewDataSource协议,并实现协议方法
funccollectionSkeletonView(_skeletonView:UICollectionView,cellIdentifierForItemAt indexPath:IndexPath)->ReusableCellIdentifier{return"MovieHomeCell"}funccollectionView(_collectionView:UICollectionView,cellForItemAt indexPath:IndexPath)->UICollectionViewCell{letcell=collectionViewdequeueReusableCell(withReuseIdentifier:"MovieHomeCell",for:indexPath)as!MovieHomeCellletmodel=selfmodels[indexPathrow]ifmodelvod_picisEmpty==false{cellimageViewkfindicatorType=activity cellimageViewkfsetImage(with:ImageResource(downloadURL:URL(string:modelvod_pic!)!),placeholder:UIImage(named:"zanwutupian"))}celltitleLabeltext=modelvod_namereturncell}// 显示骨架cell的个数funccollectionSkeletonView(_skeletonView:UICollectionView,numberOfItemsInSection section:Int)->Int{return12}
42 设置视图skeletonables
classMovieHomeCell:DiaryBaseCollectionViewCell{/// varimageView=UIImageView()/// 标题vartitleLabel=UILabel()overridefuncconfig(){selfisSkeletonable=trueselfcontentViewlayercornerRadius=10selflayercornerRadius=10selfcontentViewclipsToBounds=trueselfcontentViewaddSubview(selftitleLabel)selftitleLabelfont=LCZBoldFontSize(size:16)selftitleLabelsnpmakeConstraints({(make)inmakebottomequalToSuperview()offset(-10)makeleftequalToSuperview()offset(10)makerightequalToSuperview()offset(-10)})selftitleLabeltextAlignment=centerselftitleLabelfont=LCZFontSize(size:14)selftitleLabeltextColor=LCZHexadecimalColor(hexadecimal:"#FECE1D")selftitleLabelisSkeletonable=trueselftitleLabeltext="乐淘视界"selfcontentViewaddSubview(selfimageView)selfimageViewcontentMode=scaleAspectFillselfimageViewclipsToBounds=trueselfimageViewsnpmakeConstraints{(make)inmakelefttoprightequalToSuperview()makebottomequalTo(selftitleLabelsnptop)offset(-10)}selfimageViewisSkeletonable=true}}
33 显示骨架
viewisSkeletonable=trueselfmoreMoviesViewcollectionViewprepareSkeleton(completion:{doneinselfviewshowAnimatedGradientSkeleton(usingGradient:SkeletonGradient(baseColor:UIColorclouds),animation:GradientDirectiontopLeftBottomRightslidingAnimation())})
4人点赞
日记本
这种方法一般用于tableview,制作类似于微博浏览,空间展示动态的功能。表的高度根据以及字符串字数确定。
通过文字展示的最大宽度以及文字字体大小确定文字展示的高度
[objc] view plain copy
- (CGRect)getHeightOfText:(NSString )text width:(CGFloat)width font:(UIFont )font{
/
width:设定的字符串展示的宽度
font :字符的字体大小
/
CGRect rect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];
/
字符串对应高度
float aHeifht = rectsizeheight;
字符串对应宽度
float aWidth = rectsizewidth;
/
return rect;
}
根据需要展示的高度或者文字高度自适应表行高
[objc] view plain copy
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{
if (文字内容){
NSString text = [NSString stringWithFormat:@"\t%@",需要展示的文字];
CGRect rect = [self getHeightOfText:text width:300 font:[UIFont systemFontOfSize:14]];
return rectsizeheight+10;
}else//展示
{
return [[ objectForKey:@"height"] integerValue]+10;//获取服务器中高度,为了美观都比原始高多10
}
}
设置cell内容
[objc] view plain copy
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath{
NewsDetailCell cell = [tableView dequeueReusableCellWithIdentifier:@"data"];
NSDictionary content = [_dataArray objectAtIndex:indexPathrow];
if ([[content objectForKey:@"data_type"] intValue] == 1) {
//文本
NSString text = [NSString stringWithFormat:@"\t%@",[content objectForKey:@"content"]];
celltxtLabeltext = text;
CGRect rect = [CommonTool getHeightOfText:text width:300 font:[UIFont systemFontOfSize:14]];
celltxtLabelframe = CGRectMake(10, 5, rectsizewidth, rectsizeheight);
}else{
//
NSString urlString = [NSString stringWithFormat:@"%@%@",SERVER_ADDRESS,[[content objectForKey:@"image"] objectForKey:@"source"]];
[cellnewsImageView setImageWithURL:[NSURL URLWithString:urlString]];
cellnewsImageViewframe = CGRectMake(10, 5, 300, [[[content objectForKey:@"image"] objectForKey:@"height"] integerValue]);
}
return cell;
}
以上就是关于选中的那个cell,并修改它的内容呢全部的内容,包括:选中的那个cell,并修改它的内容呢、indexpath.row返回的什么值、如何获取UITableView所有的cell等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)