我有一個表視圖。和IM添加兩個按鈕,每個單元:以編程方式添加UIButton時出現UITableView錯誤
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
newBtn = [[UIButton alloc]init];
newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[newBtn setFrame:CGRectMake(250,10,25,55)];
[newBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
[newBtn setTitle:@"+" forState:UIControlStateNormal];
[newBtn setEnabled:YES];
newBtn.hidden = YES;
[cell addSubview:newBtn];
subBtn = [[UIButton alloc]init];
subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[subBtn setFrame:CGRectMake(280,10,25,55)];
[subBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside];
[subBtn setTitle:@"-" forState:UIControlStateNormal];
[subBtn setEnabled:YES];
subBtn.hidden = YES;
[cell addSubview:subBtn];
}
return cell;
}
,我想有隱藏在第一按鈕,那麼當表處於「編輯」模式,我想這些按鈕出現。當表格離開「編輯」模式時,按鈕消失。
我可以得到一個單元格按鈕來做到這一點。
- (IBAction)editButton:(id)sender
{
if (self.editing)
{
[self setEditing:NO animated:YES];
[self.myTableView setEditing:NO animated:YES];
EditButton.title = @"Edit";
subBtn.hidden = YES;
newBtn.hidden = YES;
}
else
{
[self setEditing:YES animated:YES];
[self.myTableView setEditing:YES animated:YES];
EditButton.title = @"Done";
subBtn.hidden = NO;
newBtn.hidden = NO;
}
}
但問題是:當我這樣做時,只有非常最後的單元格獲取按鈕。它們在我想要的時候出現和消失,但只有最後一個細胞!沒有其他細胞得到任何按鈕,有人可以幫助我!非常感謝!
你能解釋一下這個好一點嗎?我是一個新手!大聲笑你是什麼意思的「subcall」?如果它沒有太多的麻煩,請你請提供一些示例代碼,這樣我可以研究它並在我的程序中使用它?謝謝您的幫助! – iProRage 2012-01-10 23:38:32
另外,我的按鈕如何「點」到最後一個單元格的按鈕?我不知道它是什麼代碼!再次感謝! :D – iProRage 2012-01-12 00:58:50
我的意思是'子類'。看看你每次做「subBtn = [[UIButton alloc] init];」你的指針顯然被覆蓋,所以最後它指向最後一個單元格的按鈕。不知道如何更好地解釋這一點。 – 2012-01-12 04:37:04