为了向可用于标识行的单元格添加属性,我创建了一个自定义单元格,该单元格是从UITableViewCell子类化的。该部分似乎工作正常,但是当我尝试实现didSelectRowAtIndexPath时,我无法获取自定义单元格指针,因此无法访问该属性。我是否需要覆盖cellForRowAtIndexPath?
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyReuseIdentifier";
NLSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[NLSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
NSLog(@"indexPath: %ld", indexPath.row);
NSDictionary *cellDict = [self.sql getTitleAndIdForRow:indexPath.row];
NSArray *keys = [cellDict allKeys];
id rowId = [keys objectAtIndex:0];
id title = [cellDict objectForKey:rowId];
cell.textLabel.text = title;
cell.rowId = (NSUInteger)rowId;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//This message returns a UITableViewCell, not a my custom NLSTableViewCell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *string = [[NSString alloc] init];
string = cell.textLabel.text;
NSUInteger rowId = cell.rowId;
NSLog(@"id: %ld, title: %@", rowId, string);
}
铸造怎么样?
NLSTableViewCell *cell = (NLSTableViewCell *) [tableView cellForRowAtIndexPath:indexPath];
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句