2013-06-20 1 views
0

내 맞춤 검색 버튼을 만들고 아래 코드와 같이 버튼 번호를 제공하고 있습니다. 나는 10 개의 행이있는 테이블을 가지고 있고 행 1-5 버튼을 터치 할 때 나는 내 로그uitabelview 맞춤 검색 버튼 태그 문제

[button tag]: 100 ... 
[button tag]: 101 ... 
[button tag]: 102 ... 
[button tag]: 103 ... 
[button tag]: 104 ... 

에서 다음을받을 경우

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    //NSLog(@"Inside cellForRowAtIndexPath"); 

    static NSString *CellIdentifier = @"Cell"; 

    // Try to retrieve from the table view a now-unused cell with the given identifier. 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // If no cell is available, create a new one using the given identifier. 
    if (cell == nil) 
    { 
     // Use the default cell style. 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

     button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [button addTarget:self 
        action:@selector(selectPicOnBtnTouch:) 
     forControlEvents:UIControlEventTouchDown]; 
     [button setTitle:@"btn1" forState:UIControlStateNormal]; 
     button.frame = CGRectMake(6.0f, 0.0f, 97.0f, 110.0f); 
     button.tag = (indexPath.row)+100; 
     [cell addSubview:button]; 

     [button setImage:[UIImage imageNamed:@"sample_pic.png"] forState:UIControlStateNormal]; 

    } 
    else 
    { 
     //NSLog(@"went here 2"); 
     button = (UIButton *) [cell viewWithTag:((indexPath.row)+100)]; 
    } 

    //Get an arrow next to the name 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    return cell; 

} 

- (IBAction) selectPicOnBtnTouch:(id)sender 
{ 
    button = (UIButton *)sender; 
    NSLog(@"[button tag]: %d ...", [button tag]); 
} 

이 괜찮습니다. 문제는 내가 행 6-10을 터치하면 내가 105 개 기대하고 있다는 것입니다 - 시작 동일한 태그 번호를 109 태그를하지만 내가 돌아올 것은

나는 독특한 (그리고 반복하지 태그 번호를 유지하려는 이유

[button tag]: 100 ... 
[button tag]: 101 ... 
[button tag]: 102 ... 
[button tag]: 103 ... 
[button tag]: 104 ... 

이상) 그래서 어떤 버튼이 어떤 행에 해당하는지 알 수 있습니다. 어떻게해야합니까?

답변

2

그것 때문에 UITableView의 각 행에 대해 새 셀을 만들므로주의해야합니다.

하지만 사용하는 것이 더 좋습니다 UITableView 행 수가 제한적일 때 (약 50-100 일 수 있음) 다음 코드가 도움이 될 경우 사용하십시오.

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     /// Put your code here 
    } 

    return cell; 
} 
+1

+1은 메모리 관리에 대한 설명입니다. 많은 사용자가 메모리 관리에 나쁜 영향을 줄 때도 알려주지 않습니다. – Popeye

+0

@ Popeye- 초심자에게 감사합니다. :) – iPatel

+1

많은 사용자가 재사용 가능한 셀을 이해하지 못하고 문제를 일으키지 않고 원하는만큼 많은 셀을 만들 수 있다고 생각합니다. – Popeye

0

셀을 다시 작성한을 사용하고 셀을 다시 사용할 때 태그를 업데이트하지 않으므로 셀을 만드는 if 블록 내에 단추 태그를 설정하지 마십시오.

EDIT

: 제 5 셀if 블록이 실행되고, 새로운 세포 가 생성되고있다. 6 번째 셀 이후 셀이 재사용되고 있으므로 카운트가 증가하지 않습니다.

전화 당신이처럼 if (cell == nil) 블록 아래

button.tag = (indexPath.row)+100; 

: 당신은 내 대답을 수행하기 전에 내가 코드를 다음은 메모리 관리를 위해 나쁜 것을 당신에게 wnat

if (cell == nil) 
    { 
     //... 

    } 
    else 
    { 
     ///... 
     button = (UIButton*)[[cell subviews] lastObject]; //this assumes your button is the last view in the subview array (double check) 
    } 
    button.tag = (indexPath.row)+100; 
+0

. 셀이 재사용 될 때 100-104에서 여전히 카운트되고 –

+0

이상 - 버튼은 nil입니까? 그렇다면 셀에 들어가서 버튼 하위 뷰를 수동으로 가져와 태그를 업데이트해야합니다. – foggzilla

0

당신은 그런 식으로 떨어지게을 수행해야합니다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.rNewsCell = [self.tabelView dequeueReusableCellWithIdentifier:allNewsCellIdentifier forIndexPath:indexPath]; 

    if (self.news.count != 0) 
    { 
     if(indexPath.row <[self.news count]) 
     { 
      RNewsModel *news = (self.news)[indexPath.row]; 
      self.rNewsCell.newsImage.contentMode = UIViewContentModeScaleAspectFill; 
      self.rNewsCell.newsName.text = news.newsNameModel; 
      self.rNewsCell.newsData.text = news.newsDataModel; 
      self.rNewsCell.newsWatches.text = news.newsWatchesModel; 
      self.rNewsCell.newsAddress.text = news.newsAddressModel; 
      self.rNewsCell.newsTime.text = news.newsTimeModel; 
      self.rNewsCell.playFrame.image = [UIImage imageNamed: news.newsPlayImage]; 
      self.rNewsCell.playButton.tag = indexPath.row; 
      self.rNewsCell.showOnMap.tag = indexPath.row; 

      self.rNewsCell.rightUtilityButtons = [self rightButtons]; 
      self.rNewsCell.delegate = self; 
     } 
    } 
    return self.rNewsCell; 
} 

그 후

버튼 액션을 잡을 : 난 그냥했고, 그 차이를하지

- (IBAction)showNewsOnTheMap:(id)sender 
{ 
    UIButton *button=(UIButton *) sender; 
    self.showNewsOnMap = [NSIndexPath indexPathForRow:button.tag inSection:0]; 
    RNewsModel *news = (self.news)[self.showNewsOnMap.row]; 
    NSLog(@"didSelect NEWSOnMap = %@",news.newsIDModel); 
    [self performSegueWithIdentifier:SegueShowNewsOnTheMap sender:self]; 
}