国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

外部獲取IndexPath的幾種方式(關聯對象等)

2019-11-09 17:08:29
字體:
來源:轉載
供稿:網友

一、單擊某個cell中的button獲取indexPath

1、 一般方式
- (void)buttonAction:(UIButton *)sender { UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview]; NSIndexPath *indexPath = [_tableView indexPathForCell:cell]; NSLog(@"indexPath is = %i",indexPath.row); }
2、runtime添加屬性方式,即關聯對象的方式
//runtime 關聯對象 這種方式首先引入#import <objc/runtime.h> - (UITableViewCell *)tableView:(UITableView *)tableVie cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identiStr = @"cellID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identiStr]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identiStr]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, 100, 33); [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; button.tag = 110 + indexPath.row; [cell.contentView addSubview:button]; } UIButton *button = (UIButton *)[cell.contentView viewWithTag:110]; //runtime 關聯對象 objc_setAssociatedObject(button, @"button", indexPath, OBJC_ASSOCIATION_ASSIGN); [button setTitle:dataSource[indexPath.row] forState:UIControlStateNormal]; return cell; } //事件觸發 runtime 獲取關聯的對象 - (void)buttonAction:(UIButton *)sender { //runtime 獲取關聯的對象 UITableViewCell *cell = objc_getAssociatedObject(sender, @"button"); NSIndexPath *indexPath = [_tableView indexPathForCell:cell]; NSLog(@"indexPath is = %ld",indexPath.row); }

二、已知具體row,獲取indexPath

- (void) refreshLessTime{ for (int row = 0; row < leftTimeArr.count; row ++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem: row inSection:0]; UITableViewCell *cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath]; UILabel *remainingTimeLabel = (UILabel *)[[cell.contentView viewWithTag:500] viewWithTag:501]; remainingTimeLabel.text = [leftTimeArr objectAtIndex:indexPath.row]; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 准格尔旗| 芦溪县| 长子县| 海盐县| 原平市| 垦利县| 宿松县| 疏勒县| 河西区| 霍山县| 江都市| 东源县| 根河市| 屏山县| 岳池县| 兴业县| 康乐县| 晋宁县| 宜宾市| 阿拉善右旗| 宁远县| 望江县| 新化县| 通榆县| 石渠县| 长岭县| 郓城县| 普兰县| 正镶白旗| 遵义县| 沧源| 沙湾县| 玛沁县| 阜康市| 白玉县| 邯郸市| 蒙阴县| 古浪县| 山东省| 仁化县| 荔波县|