2014-10-04 3 views
0

У меня есть кнопка курса. При нажатии на эту кнопку открывается вид, содержащий 5 звезд. Но когда я нажимаю кнопку курса определенной ячейки, открывается конкретный вид, но некоторые виды других ячеек также становятся открытыми. Я хочу, чтобы представление, содержащее 5 звезд этого конкретного вида, было открыто.IOS: Как предотвратить просмотр от открытия во всех ячейках

У меня есть код этого вида.

cv = [[ASStarRatingView alloc]initWithFrame:CGRectMake(190, y-5, 140, 30)]; //creat an instance of your custom view 


cv.tag = indexPath.row ; 
if([delegate.constants.snsrate[indexPath.row] isEqualToString: @"0"]) 
{ 
     cv.hidden=TRUE; 
} 
else 
{ 
     cv.hidden=FALSE; 
} 

delegate.constants.selected=1; 
[cell addSubview:cv]; 

И мой код, чтобы закрыть и открыть представление.

-(void)Rate_Comment:(UIButton *)sender 
{ 



    UIButton *b = (UIButton *)sender; 

    NSInteger row = b.tag ; 

    for(int i=0;i<[delegate.constants.snsrate count];i++) 
     delegate.constants.snsrate[i]= @"0"; 


    delegate.constants.buttontag = row; 

    UITableViewCell *buttonCell = (UITableViewCell *)[b superview]; 
    UITableView* table = (UITableView *)[buttonCell superview]; 
    NSIndexPath* pathOfTheCell = [table indexPathForCell:buttonCell]; 
    delegate.constants.snsIndexPath = pathOfTheCell; 
    if(![delegate.constants.user_id isEqualToString:[delegate.constants.snsCreatedBy objectAtIndex:row]]) 
       { 

        if([[delegate.constants.snstotalrating_count objectAtIndex:row] isEqualToString:@"0"]) 
        { 

         if(cv.hidden) 
         { 


          delegate.constants.snsrate[row]= @"1"; 

          cv.hidden=FALSE; 

         } 
         else{ 


          delegate.constants.snsrate[row]= @"0"; 

           cv.hidden=TRUE; 
         } 


[streamTable reloadRowsAtIndexPaths:@[pathOfTheCell] withRowAnimation:UITableViewRowAnimationNone]; 

      }else 
       { 
       if(cv.hidden==FALSE) 
       cv.hidden=TRUE; 

      } 
    } 
} 

Я новичок в IOS. Пожалуйста, если кто-то может помочь, он будет очень признателен. Спасибо заранее.

ответ

0

Я предполагаю, что каждая клетка имеет кнопку «скорость» и целевой метод, как это:

// -------------------------------------------------------------------- 
// Sorry, I'm typing from memory, these code might not be 100% correct 
// -------------------------------------------------------------------- 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"cell"; 

    MyCustomCell *cell = [tableView dequeueReusableCellForIdentifier:cellID]; 

    if(cell == nil) 
    { 
     cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; 

     // -------------------------------------------------------- 
     // Add the method to execute when tapping on a button 
     // -------------------------------------------------------- 
     [cell.btnRate addTarget:self action:@selector(showRateControl:) forControlEvent:UIControlEventTouchUpInside]; 
    } 

    // self.selectedRow should init with -1, since we don't want to show stars 
    // for the first row if self.selectedRow defaults to 0 
    if(self.selectedRow == indexPath.row) 
    { 
     cell.starsView.alpha = 1; 
     cell.btnRate.alpha = 0; 
    } 
    else 
    { 
     cell.starsView.alpha = 0; 
     cell.btnRate.alpha = 1; 
    } 

    return cell; 
} 

-(void)showRateControl:(id)sender 
{ 
    UIButton *rateButton = (UIButton *)sender; 

    // finding the row which the rate button was tapped 
    CGPoint rootPoint = [rateButton convertPoint:CGPointZero inView:self.tableView]; 

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:rootPoint]; 


    // ------------------------------------------------------------ 
    // here we record the row which the rate button was tapped 
    // in a property we declared in the .h file (see below) 
    // ------------------------------------------------------------ 
    self.selectedRow = indexPath.row; 

    // refresh just the selected row (you could alternatively use self.tableView reloadData) 
    [self.tableView reloadRowsAtIndexPath:@[indexPath]]; 
} 

В файле заголовка, вы можете объявить свойство selectedRow так:

... 

class MyViewController: UIViewController 
{ 

} 

@property (nonatomic, assign) NSInteger selectedRow; 

@end 

в viewDidLoad вашего файла реализации, то просто не забудьте инициализирует self.selectedRow -1:

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    ... 

    self.selectedRow = -1; 
} 

Посмотрите, поможет ли это.

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