簡體   English   中英

tableview viewWithTag沒有檢索UIElements objective-c

[英]tableview viewWithTag not retrieving UIElements objective-c

我正在使用一些我以前見過的代碼。 基本上用戶在帶有一些按鈕的帖子上回答是或否。 按是或否更新正常工作的數據庫,它還會更新可見的UI,這不起作用。 此UI更新按鈕,以便選擇一個按鈕,突出顯示其他按鈕,並禁用這些按鈕以進行用戶交互。 它還對兩個UILabel進行了更改。 這些按鈕調用的方法需要更新數據庫並從tableViewCell檢索按鈕並更新我在另一個ViewController中使用的方法的更改,所以我無法理解這里的區別。 這是我的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row];
NSLog(@" simple: %@",simpleTableIdentifier);
if (indexPath.row==0) {
    ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:simpleTableIdentifier];
    }


    cell = [self createProfileCell:cell];
    return cell;

}else{
    YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell==nil) {
        cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell = [self createYesNoCell:cell:indexPath];
    return cell;

  }
}

基本上它的作用是在第一個單元格中創建用戶配置文件,並加載用戶要求的所有問題。 這是我在舊tableView和this tableView之間看到的主要區別。 在createYesNoCell中,我創建UIElements並創建標簽,如下所示

    cell.yesVoteButton.tag=indexPath.row+ yesVoteButtonTag1;
    cell.noVoteButton.tag=indexPath.row+ noVoteButtonTag1;
    cell.yesCountLabel.tag=indexPath.row+ yesCountLabelTag1;
    cell.noCountLabel.tag=indexPath.row+ noCountLabelTag1;

按鈕具有啟動許多功能的選擇器。 它會找到以下按下的按鈕。

     NSInteger index;
if(sender.tag>=yesVoteButtonTag1){
    NSLog(@"Yes button pressed");
    votedYes=true;
    index=sender.tag-yesVoteButtonTag1;

}else{
    NSLog(@"No button Pressed");
    votedYes=false;
    index=sender.tag-noVoteButtonTag1;
}

     UILabel *yesLabel = (UILabel*) [self.tableView   viewWithTag:index+yesCountLabelTag1]; // you get your label reference here
     UIButton *yesButton=(UIButton *)[self.tableView viewWithTag:index+1+yesVoteButtonTag1];
     NSLog(@"Tag IN METHOD: %ld",index+yesVoteButtonTag1);
     UILabel *noLabel = (UILabel*) [self.tableView viewWithTag:index+1+noCountLabelTag1]; // you get your label reference here
     UIButton *noButton=(UIButton *)[self.tableView viewWithTag:index+noVoteButtonTag1];

當我看到它們時,這些viewWithTag調用是零。 我從之前的實現中看到的唯一區別是舊的有部分和一行,而這一行是所有行和一個部分。 因此,用indexPath.row替換indexPath.section應該考慮到這一點。 另外,我檢查了在cellForRowAtIndexPath中生成的標記與在yes / no投票方法中恢復的行相同,因為由於在indexPath.row == 0處創建了配置文件單元格而將其替換為1。 我嘗試將單元格傳遞給是/否投票方法並嘗試使用contentView恢復按鈕和標簽,作為對類似帖子的一些建議。 然而,這似乎並沒有解決我的問題。 真的很感激對此的一些見解。

你有沒有調用'[tableView reload]'方法來更新UITableView,它可能有所幫助。

首先,表重用標識符應該用於單元格類型 ,而不是每個單元格一個。 您有兩種類型,因此您應該使用兩個固定的重用標識符。

ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ProfileCell"];

if (cell == nil) {
    cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:@"ProfileCell"];
}

YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:@"YesNoCell"];
if (cell==nil) {
    cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YesNoCell"];
}

其次,在創建不適合您的表之后,不應嘗試獲取對單元格的引用,而應在創建單元格時完全初始化單元格。 (TableView不會創建單元格,除非它們可見,所以你不應該依賴它們的存在。)

createProfileCell實際上應該被稱為initializeProfileCell ,因為你沒有在其中創建單元格 - 你已經在上面的行中執行了這個操作,或者恢復了舊的單元格。

然后,您對initializeProfileCell的調用可以獲取一個標志,指定它是Yes還是No單元格並相應地設置其屬性。

cell = [self initializeProfileCell:cell isYes:(indexPath.section==0)];

createYesNoCell類似 - > initializeYesNoCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    static NSString *CellIdentifier = @"YOURCELL_IDENTIFIER";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
   UILabel *title = (UILabel*) [cell viewWithTag:5];
   UILabel *vensu =(UILabel*) [cell viewWithTag:7];
   vensu.text = @"YOUR TEXT";

   title.text = @"YOUR TEXT";

   return cell;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM