2013-06-02 65 views

回答

1

你可以让你的电池的按钮属性,并在cellForRowAtIndexPath可以设置目标加载表中的类查看,所以你不需要任何代表。这样的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *identifier = @"YourCellIdentifier"; 
     YourCustomCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if(cell == nil) { 
      cell = [YourCustomCell alloc/init method.... 
      [cell.buttonProperty addTarget:self action:@selector(cellButtonTapped:) 
            forControlEvents:UIControlEventTouchUpInside]; 
     } 
     //do other stuff with your cell 
} 

-(void)cellButtonTapped:(id)sender { 
    UIButton *button = (UIButton*)sender; 
    YourCustomCell *cell = (YourCustomCell*)button.superview.superview; //if the button is added to cell contentView or button.superview is added directly to the cell 

    NSIndexPath *path = [yourTableView indexPathForCell:cell]; 
    [yourTableView scrollToRowAtIndexPath:path 
         atScrollPosition:UITableViewScrollPositionTop 
           animated:YES]; 
} 
1

建立一个委托机制返回从你的单元格 - 当创建单元格时,为它指定一个NSIndexPath,并在点击该按钮时从单元格传回它。

因此,在你的UITableViewCell子类中,你将有:

- (IBAction)buttonPressed:(id)sender 
{ 
    [self.delegate buttonPressedOnCellAtIndexPath:cell.indexPath] 
} 

回到那就是代表控制器,请回覆此法:

- (void)buttonPressedOnCellAtIndexPath:(NSIndexPath)indexPath 
{ 
    [self.tableView scrollToRowAtIndexPath:indexPath]; 
} 
+0

delegationtain mechanism?有更容易的方法吗?我其实不需要通过任何方法。我只想跳到某个部分。 – DanielR

+0

你能告诉我应该把什么放在我的.h文件中,以便代表团正常工作吗? – DanielR

2

您可以滚动到某个部分与使用此功能:

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated 

使用示例为:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:indexPath.section] 
      atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; 

和链接与实现代码如下按钮动作,你可以在你使用协议自定义单元格