2013-03-29 24 views
8

Tôi muốn kết nối một ô tĩnh với một hành động bằng cách sử dụng bảng phân cảnh. Vấn đề là bạn không thể kết nối các tế bào với một hành động vì vậy tôi đã thử nó một cách khác. Vì vậy, trong tập tin tiêu đề của tôi, tôi đã kết nối khách sạn này với các tế bào tĩnh nhờ sủ dụng storyboards:Kết nối ô tĩnh với một hành động

@property (nonatomic, strong) IBOutlet UITableViewCell *theStaticCell; 

UITableViewCell *theCellClicked = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; 
if (theCellClicked == _theStaticCell) { 
    NSLog(@"Static cell clicked"); 
} 

Vì vậy, tôi muốn sử dụng "Cập nhật" tế bào, rằng khi bạn nhấp vào nó mã trên được thực hiện.

enter image description here

Trả lời

14
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section ==1 && indexPath.row == 0) 
    { 
     //Do what you want to do. 
    } 
} 

HOẶC

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Cell will be deselected by following line. 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UITableViewCell *staticCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; 

    if (cell == staticCell) 
    { 
     //Do what you want to do. 
    } 
} 
+0

vấn đề là khi tôi nhấp vào ô, nền từ ô đó biến thành màu xanh và điều này không biến mất, làm thế nào tôi có thể để điều này biến mất? –

+1

@nonuma xem câu trả lời đã chỉnh sửa. – viral

+0

Tôi nên làm như thế nào nếu tôi đang sử dụng bảng phân cảnh? –

3

tôi nghĩ rằng thay vì kết nối nó với tế bào tĩnh bạn nên sử dụng TableView Đại biểu Phương pháp

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Put your action logic here by using its index "indexPath.row". 
} 
1
#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([tableView cellForRowAtIndexPath:indexPath] == self.theStaticCell){ 
     NSLog(@"Static cell clicked"); 
    } 
} 
Các vấn đề liên quan