大家好, 我正在嘗試添加與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