2011-06-12 55 views
0

我有一個問題,我希望你們其中一個可以提出一個更好的方法來完成我想要做的事情。添加選中標記圖像到選中的同一行UITableView

所以我有一個UITableView的項目列表。每個tableview行都有一個「添加」按鈕。當按下添加按鈕時,所選項目被添加到主列表。 (想到一個待辦事項應用程序),這工作正常,沒有問題。我想要的是該項目被添加到列表中的某種類型的通知。例如,在添加了兩秒鐘的同一行上出現的複選標記隨即消失。這裏是我的代碼:

-(void)addToList:(id)sender event:(id)event{ 

//This is the add button method on each cell. 

NSSet *touches = [event allTouches]; 
UITouch *touch = [touches anyObject]; 
CGPoint currentTouchPosition = [touch locationInView:self.theTableView]; 
NSIndexPath *indexPath = [self.theTableView indexPathForRowAtPoint: currentTouchPosition]; 

//Check image is the image of the checkmark that I want to display when the user adds an item. 

checkImage.alpha = 0.0f; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.7f]; 

checkImage.alpha = 1.0f; 

checkImage.frame = CGRectMake(currentTouchPosition.x + 100, currentTouchPosition.y, 25, 25); 

[UIView commitAnimations]; 

....[only relevant code shown]... 

} 

當用戶點擊添加按鈕,在checkImage淡入,並對其定位自身在用戶觸摸行+ 100個像素的權利。這可以正常工作,但不會將複選標記放置在行的中心,它可能位於中心,靠近頂部或靠近底部,具體取決於用戶點按添加按鈕的位置。我需要在中心,或以不同的方式去解決這個問題。有什麼建議麼?謝謝!

+0

如何將'checkImage'添加到表視圖? – 2011-06-12 23:40:06

+0

checkImage只是一個普通的UIImageView,它只是位於視圖之上,它並不是專門添加到tableview中的。 – Ryan 2011-06-12 23:47:25

+0

用戶滾動時會發生什麼?你不會以這種方式失去你的位置嗎? – 2011-06-12 23:48:43

回答

0

一旦你得到了indexPath,使用獲得的細胞,

UITableViewCell * cell = [self.theTableView cellForRowAtIndexPath:indexPath]; 
CGSize cellSize = cell.contentView.frame.size; 

後來,

CGPoint touchPositionInCell = [self.theTableView convertPoint:currentTouchLocation toView:cell.contentView]; 

CGPoint checkmarkCenter = CGPointMake(touchPositionInCell.x + 100, cellSize.height/2); 
CGRect checkmarkFrame = CGRectZero; 
checkmarkFrame.origin = checkmarkCenter; 
checkmarkFrame = CGRectInset(checkmarkFrame, -12, -12); 

checkImage.frame = checkmarkFrame; 

[cell.contentView addSubview:checkImage]; 

這應該在垂直位置中間的對號。

我希望您瞭解可重複使用細胞的概念。嘗試記住checkImage所在單元格的indexPath。如果您在該indexPath處看到對單元格的請求,請將checkImage作爲其子視圖。

+0

輝煌。看起來我有一些學習要做!完美解決方案非常感謝Deepak,你的傳奇。 – Ryan 2011-06-13 00:06:19

+0

這是一個偉大的,但我有問題,我想添加圖像選定的行取決於條件,但有問題。我已經在這裏發佈的問題http://stackoverflow.com/questions/8194934/how-to-load-images-to-seletcted-rows-in-a-uitableview/8195751#8195751請分享任何想法。 – Mithuzz 2011-11-22 04:34:02

相關問題