2015-06-25 4 views
1

Я создал пользовательское Tableview displayCustomUnitTableView и пользовательские UIView DisplayView и добавление displayCustomUnitTableView к DisplayView. Но я не могу выбрать любую строку в таблицеView, т. Е. didSelectRowAtIndexPath не вызывается.после добавления UITableView в UIView didselectrowatindexpath не называется Ios

Я использую следующий код:

displayCustomUnitTableView = [[UITableView alloc]initWithFrame:CGRectMake(52.0, 110.0, 180.0, 110.0) style:UITableViewStylePlain]; 
displayCustomUnitTableView.delegate = self; 
displayCustomUnitTableView.dataSource = self; 
displayCustomUnitTableView.tag = 2; 
displayCustomUnitTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
displayCustomUnitTableView.separatorColor = [UIColor blackColor]; 
displayCustomUnitTableView.rowHeight = 30.f; 
[displayCustomUnitTableView setBackgroundColor:[ContentViewController colorFromHexString:@"#BCC9D1"]]; 
[displayCustomUnitTableView setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; 
[displayCustomUnitTableView setAllowsSelection:YES]; 
[displayView addSubview:displayCustomUnitTableView]; 

cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"MyCell"; 
    UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath]; 
    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    if(tableView.tag == 1) 
    { 
    cell.textLabel.text = [storeUnitList objectAtIndex:indexPath.row]; 
    } 
    else if(tableView.tag == 2) 
    { 
    cell.textLabel.text = [storeCustomUnitList objectAtIndex:indexPath.row]; 
    } 
    return cell; 
} 

didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(tableView.tag == 1) 
{ 
    unitString = [storeUnitList objectAtIndex:indexPath.row]; 
} 
else if(tableView.tag == 2) 
{ 
    unitString = [storeCustomUnitList objectAtIndex:indexPath.row]; 
} 
} 

Спасибо заранее.

+0

Можете ли вы показать, где вы внедрили делегатов? – GoodSp33d

+0

Пожалуйста, проверьте, что я редактировал мой пост – VJVJ

+0

Ваш код отлично работает в моем проекте xcode. Здесь работает didSelectRowAtIndexPath. требуется только нужный делегат numberOfRowsInSection из TableView. –

ответ

2

Убедитесь, что у вас нет никаких UIGestures о superviews. В этом случае UITableView не будет получать штрихи.

Если у вас есть, обрабатывать его с помощью:

- gestureRecognizer:shouldReceiveTouch: метод делегата.

+0

Спасибо, сработало. – VJVJ

0

попробовать этот Regiseter класса клеток с Tabel

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    displayCustomUnitTableView = [[UITableView alloc]initWithFrame:CGRectMake(52.0, 110.0, 180.0, 110.0) style:UITableViewStylePlain]; 
displayCustomUnitTableView.delegate = self; 
displayCustomUnitTableView.dataSource = self; 
displayCustomUnitTableView.tag = 2; 
displayCustomUnitTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
displayCustomUnitTableView.separatorColor = [UIColor blackColor]; 
displayCustomUnitTableView.rowHeight = 30.f; 
[displayCustomUnitTableView setBackgroundColor:[ContentViewController colorFromHexString:@"#BCC9D1"]]; 
[displayCustomUnitTableView setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; 
[displayCustomUnitTableView setAllowsSelection:YES]; 
[displayView addSubview:displayCustomUnitTableView]; 

    [displayCustomUnitTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"identifier"]; 

} 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 1;} 



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"i" forIndexPath:indexPath]; 

     /* 

     Write code 

     */ 
     cell.textLabel.text= @"hello"; 


     return cell; 

    } 
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
     NSLog(@"Click"); 
    } 
+0

все еще не получилось вызвано – VJVJ

+0

вы уверены, что вы зарегистрировали класс ячейки с таблицей –

+0

[displayCustomUnitTableView registerClass: [UITableViewCell class] forCellReuseIdentifier: @ "identifier"]; –

Смежные вопросы