大家好, 我正在尝试添加与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];
}
请勿在您的帖子中使用签名或标语,否则将被删除。有关详细信息,请参见[常见问题](http://stackoverflow.com/faq#signatures)。 – Artemix