【发布时间】:2014-05-02 14:18:36
【问题描述】:
我有一个 UITableView,我用它来显示需要我拥有比 iPhone 屏幕宽度更宽的单元格的数据。
因此,我创建了一个包含子类 UIScrollView 的子类 UITableViewCell。这就是我的代码的样子。
TableView.h
@property (strong, nonatomic) CustomCell *cell;
TableView.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cellDictionary = [xmlMArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.itemsDictionary = cellDictionary;
cell.currentIndex = indexPath;
cellDictionary = nil;
[cell drawCell];
return cell;
}
在上述方法中,您可以看到我正在调用我创建的自定义 UITableViewCell。
CustomCell.h
@property (strong, nonatomic) NSDictionary *itemsDictionary;
@property (strong, nonatomic) MyScrollView *scrollCell;
@property (strong, nonatomic) NSIndexPath *currentIndex;
- (void)drawCell;
CustomCell.m
- (void)drawCell
{
scrollCell = [[MyScrollView alloc] init];
scrollCell.itemsDictionary = itemsDictionary;
[scrollCell drawCell];
scrollCell.backgroundColor = [UIColor whiteColor];
scrollCell.frame = CGRectMake(0.0, 0.0, ScreenWidth, 45.0);
[[self contentView] addSubview:scrollCell];
}
在上面的drawCell方法中我调用了一个自定义的scrollView
MyScrollView.m
- (void)drawCell
{
bgGray = NO;
fNameString = [[UILabel alloc] init];
fNameString.backgroundColor = [UIColor clearColor];
fNameString.frame = CGRectMake(15.0, 0.5, 70.0, 40.0);
fNameString.text = [itemsDictionary objectForKey:@"fName"];
lNameString = [[UILabel alloc] init];
lNameString.backgroundColor = [UIColor clearColor];
lNameString.frame = CGRectMake(105.0, 0.5, 95.0, 40.0);
lNameString.text = [itemsDictionary objectForKey:@"lName"];
addressString = [[UILabel alloc] init];
addressString.backgroundColor = [UIColor clearColor];
addressString.frame = CGRectMake(220.0, 10.5, addressString.frame.size.width, 50.0);
addressString.text = [NSString stringWithFormat:@"Address: %@: %@",[itemsDictionary objectForKey:@"aNumber"] ,[itemsDictionary objectForKey:@"aString"]];
[addressString sizeToFit];
[self setContentSize:(CGSizeMake((220.0 + addressString.frame.size.width)+15, 45.0))];
[self addSubview:fNameString];
[self addSubview:lNameString];
[self addSubview:addressString];
}
上面的方法绘制滚动视图,然后传递到CustomCell,一切正常并正确显示,下面是我用来检测cusome滚动视图上的触摸的方法,它与上面的方法在同一个类中,看起来像这样。
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
NSLog(@"touch scroll");
// If not dragging, send event to next responder
if (!self.dragging) {
// touch detected...
//How can I now call didSelectRow from TableView?
} else {
[super touchesEnded: touches withEvent: event];
}
}
所以我的问题是,如何使用这个 touchEnded 方法来调用原始 TableView 类中的 didSelectRowFromIndexPath 方法?或者有没有比我目前正在做的更好的方法?
注意:我不得不使用这些子类的原因是因为 UIScrollView 覆盖了 UITableViewCell 选择功能,所以我必须子类化滚动视图来拦截触摸事件现在我希望我可以调用原始的 tableview 选择方法在你的帮助下。
【问题讨论】:
标签: ios objective-c uitableview uiscrollview