2012-12-23 30 views
2

大家好, 我正在尝试添加与indexPath.row相关的标签的自定义按钮。如果我不在tableview中插入新行,我可以正确查看标记值。但是,当我插入新行时,如果插入的行不在0到9之间,那么我的新行标记值是不正确的(iphone 5可以显示出来)。在iphone上,我需要向下滚动才能看到。


但是,使用相同的代码,我可以正确地在我的ipad上获得我的标记值。我不需要向下滚动我的iPad上的表格视图来查看我所有的行。我想知道为什么会发生,以及如何解决。在带有标签的tableview中添加自定义按钮

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

    static NSString *CellIdentifier = @"dialoguesTableCell"; 
    dialoguesCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[dialoguesCell alloc] initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:CellIdentifier]; 

    } 

    UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [yourButton setImage:[UIImage imageNamed:@"1StarBlank.png"]  forState:UIControlStateNormal]; 
    [yourButton setTitle:@"Button" forState:UIControlStateNormal]; 
    [yourButton addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    yourButton.tag=indexPath.row; 
    yourButton.frame = CGRectMake(5, 10, 40, 25); 
    [cell addSubview:yourButton]; 
    return cell; 


} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *indexPathstoinsert = [NSIndexPath indexPathForRow:indexPath.row+1  inSection:section]; 
    NSArray *indexPathsToInsertArray = [NSArray arrayWithObject:indexPathstoinsert]; 
    [[self mainTableView] insertRowsAtIndexPaths:indexPathsToInsertArray withRowAnimation:UITableViewRowAnimationRight]; 

} 
+0

请勿在您的帖子中使用签名或标语,否则将被删除。有关详细信息,请参见[常见问题](http://stackoverflow.com/faq#signatures)。 – Artemix

回答

1

这是不正常工作,因为UITableView的重用细胞
当你向下滚动,第一单元的arent看见了和被重用。

如果表..“小”你可以解决它通过不重复使用细胞
如果表心不是只有几个项目,但大量的数据,你真的想改变自己的方式

分配标签在按钮:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIButton *b = nil; 
    for(UIView *v in cell.subviews) { 
     if([v isKindOfClass:[UIButton class]]) { 
      b = (UIButton*)v; 
      break; 
     } 
    } 

    b.tag = indePath.row; 
} 

在commment

你提到另外一个问题:这些按钮都隐藏在你的didSelectRow方法,并从其他细胞滚动后也随后消失。同样的问题:通过tableview,单元格对象是REUSED。不要在可重复使用的单元格中存储状态!

而是有记忆状态的“模型”树或阵列:文本,图像,标签,隐藏:是/否

NSArray *myTableContents

NSMutableDictionary *d1 = [@{@"text:@"bla", @"hidden":@NO} mutableCopy]; 
NSMutableDictionary *d2 = [@{@"text:@"bloo", @"hidden":@NO} mutableCopy]; 
NSMutableDictionary *d3 = [@{@"text:@"foo", @"hidden":@NO} mutableCopy]; 
myTableContents = @[d1,d2,d3]; 

THEN始终使用数组中numberOfRows和viewForRow并在didSelectEntry中修改它

+0

谢谢。它的工作。我wana做了与这种情况有关的另一个步骤。我试图在新插入的行中隐藏该按钮。但是,当我向上或向下滚动时,其他按钮是隐藏的。我应该怎么做? - (void)tableView :(UITableView *)tableView didSelectRowAtIndexPath :(NSIndexPath *)indexPath { NSIndexPath * favouriteIndexpath = [NSIndexPath indexPathForRow:indexPath.row inSection:section]; UITableViewCell * cellktl = [tableView cellForRowAtIndexPath:favouriteIndexpath]; UIButton * exerciseDate = [cellktl viewWithTag:indexPath.row]; exerciseDate.hidden = YES; } –

+0

同样的原因,之后didSelect ..你需要一些“模型”要记住隐藏什么行,然后重新在willDisplay ..生病多写一些代码,你 –

+0

由于细胞被重用。它会工作。你能不能多展示一下如何使用这个阵列?我应该写在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {..........} 然后,我不知道什么是viewForRow。我想知道如何使用它。 –