我有一个小问题。
我像这样实现 UIableView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[cell addGestureRecognizer:longPress];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:self.FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:self.FONT_SIZE]];
[label setTag:1];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[colors getMainTextColor]];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeCellHeight:)];
[label addGestureRecognizer:tap];
[[cell contentView] addSubview:label];
这将 UITapGestureRecognizer 设置为 Label 并且它可以工作,但它总是点击第一个单元格。
方法 changeCellHeight 总是告诉我 indexPath.row = 1 我不知道为什么。
-(void)changeCellHeight:(id)sender{
UITableViewCell *cell = (UITableViewCell *)[sender view];
NSIndexPath *IndexPath = [self.table indexPathForCell:cell];
DataObject * obj_ = [self.allData objectAtIndex:IndexPath.row];
if (obj_.canChangeHeight) {
obj_.needChangeHeight = !obj_.needChangeHeight;
[self.table reloadData];
}
}
我故意把 addGestureRecognizer:tap 放在 if(cell == nil) 里面,因为我不想放弃记忆。