簡體   English   中英

需要幫助來設置自定義單元

[英]Need help setting up custom cell

我以前從未為tableView實現自定義單元,並且對如何繼續感到困惑。 我正在嘗試制作一個個性測試應用程序,其中每個問題都在tableView的單元格中表示,並且每個問題下方是幾個按鈕,這些按鈕將用作對相應問題的響應。 我設法用問題的文本視圖和帶有適當約束的幾個按鈕來設置我的單元,以使它們不會移動。

我還將屬性檢查器中的“單元格”樣式設置為“自定義”。

有人可以幫助我如何設置我的問題的文本視圖嗎? 我嘗試制作UITextView的屬性並將其鏈接到單元格中的TextView,但是隨后出現一個錯誤,提示我不允許使用IBOutlet填充重復出現的單元格。

這是我的故事板的屏幕截圖。 讓我知道您是否還有其他需要。 在此處輸入圖片說明 在此處輸入圖片說明

TestVC.h

#import <UIKit/UIKit.h>
#import "Questions.h"
#import "CustomCell.h"

@interface TestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UITextView *instructions;
@property (weak, nonatomic) IBOutlet UIButton *results;
//@property (strong, nonatomic) IBOutlet *question;
@property (nonatomic, strong) Questions *survey;

-(IBAction)backPressed:(id)sender;
@end

TestViewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [survey.questions count];
}

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

    static NSString *simpleTableIdentifier = @"QuestionCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
    //UITextView *textView = (UITextView *)[cell viewWithTag: 100];
    cell.textView = (UITextView *)[cell viewWithTag: 100];
    cell.textView.text = que;

    return cell;
}

CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

// Variables for questions and answers
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIButton *strongAgree;
@property (weak, nonatomic) IBOutlet UIButton *agree;
@property (weak, nonatomic) IBOutlet UIButton *weakAgree;
@property (weak, nonatomic) IBOutlet UIButton *neutral;
@property (weak, nonatomic) IBOutlet UIButton *strongDisagree;
@property (weak, nonatomic) IBOutlet UIButton *disagree;
@property (weak, nonatomic) IBOutlet UIButton *weakDisagree;

@end

CustomCell.m

#import "CustomCell.h"

@interface CustomCell ()

@end

@implementation CustomCell

@synthesize textView;
@synthesize strongAgree;
@synthesize agree;
@synthesize weakAgree;
@synthesize neutral;
@synthesize strongDisagree;
@synthesize disagree;
@synthesize weakDisagree;

@end

在屬性檢查器中為UITextView設置標簽,並通過viewWithTag方法獲取它:

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

    static NSString *simpleTableIdentifier = @"QuestionCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];

    UITextView *textView = (UITextView *)[cell viewWithTag:100];//Use your tag
    textView.text = que;


    return cell;
}

這應該為您工作。

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

    static NSString *simpleTableIdentifier = @"QuestionCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
    cell.textLabel.text = que;


    return cell;
}

根據我在這里看到的內容,您不會創建自定義單元,而只是創建普通的UITableViewCell

您需要創建UITableViewCell的子類。 將其命名為CustomCell或其他名稱,然后為其賦予您在故事欄中創建的IBoutlet屬性。

例如,在CustomCell.h中:

@property (weak, nonatomic) IBOutlet UITextView textView;

然后在cellForRowAtIndexPath中:

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

    static NSString *simpleTableIdentifier = @"QuestionCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    // EDIT
    if (!cell) 
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }//

    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
    cell.textview.text = que;

    return cell;
}

最后,在情節提要中,選擇您自定義的單元格,然后在檢查器中將其類設置為CustomCell。

注意:您的IBOutlet屬性應該始終是弱屬性。

暫無
暫無

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

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