테이블보기 결과가 없으면 화면에 “결과 없음”을 표시합니다. 드리겠습니다. 나는 또한 다음

나는이 tableview내가 그래서 가끔, 목록에 대한 결과가되지 않을 수 있습니다 곳, 즉 “결과가”말한다까지 뭔가를 넣어 싶습니다 어떤 결과가없는 경우 (라벨 또는 하나 개의 테이블 뷰 셀 중 하나가?).

이를 수행하는 가장 쉬운 방법이 있습니까?

나는 시도 할 label뒤에서 tableview결과에 따라 두의 다음 숨기기 하나,하지만 난 함께 일하고 있어요 때문에 TableViewController정상을하지 ViewController내가 얼마나 똑똑한 지 확실하지 않다거나 즉 드리겠습니다.

나는 또한 다음 Parse과 같이 사용 하고 서브 클래 싱하고 있습니다 PFQueryTableViewController.

@interface TableViewController : PFQueryTableViewController

필요한 추가 세부 정보를 제공 할 수 있습니다. 알려주세요.

TableViewController 스토리 보드의 장면 :

여기에 이미지 설명 입력

편집 : Midhun MP 당, 여기에 내가 사용하는 코드가 있습니다.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger numOfSections = 0;
    if ([self.stringArray count] > 0)
    {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        numOfSections                 = 1;
        //yourTableView.backgroundView   = nil;
        self.tableView.backgroundView = nil;
    }
    else
    {
        UILabel *noDataLabel         = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height)];
        noDataLabel.text             = @"No data available";
        noDataLabel.textColor        = [UIColor blackColor];
        noDataLabel.textAlignment    = NSTextAlignmentCenter;
        //yourTableView.backgroundView = noDataLabel;
        //yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.backgroundView = noDataLabel;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }

    return numOfSections;
}

그리고 여기 내가 얻은보기가 있습니다. 여전히 구분선이 있습니다. 이것이 작은 변화라는 느낌이 들지만 구분선이 왜 나타나는지 잘 모르겠습니다.

여기에 이미지 설명 입력



답변

backgroundView속성을 사용하여 쉽게 얻을 수 있습니다 UITableView.

목표 C :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger numOfSections = 0;
    if (youHaveData)
    {
        yourTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        numOfSections                = 1;
        yourTableView.backgroundView = nil;
    }
    else
    {
        UILabel *noDataLabel         = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, yourTableView.bounds.size.width, yourTableView.bounds.size.height)];
        noDataLabel.text             = @"No data available";
        noDataLabel.textColor        = [UIColor blackColor];
        noDataLabel.textAlignment    = NSTextAlignmentCenter;
        yourTableView.backgroundView = noDataLabel;
        yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }

    return numOfSections;
}

빠른:

func numberOfSections(in tableView: UITableView) -> Int
{
    var numOfSections: Int = 0
    if youHaveData
    {
        tableView.separatorStyle = .singleLine
        numOfSections            = 1
        tableView.backgroundView = nil
    }
    else
    {
        let noDataLabel: UILabel  = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
        noDataLabel.text          = "No data available"
        noDataLabel.textColor     = UIColor.black
        noDataLabel.textAlignment = .center
        tableView.backgroundView  = noDataLabel
        tableView.separatorStyle  = .none
    }
    return numOfSections
}

참조 UITableView 클래스 참조

backgroundView 특성

테이블보기의 배경보기입니다.

선언

빠른

var backgroundView: UIView?

목표 -C

@property(nonatomic, readwrite, retain) UIView *backgroundView

토론

테이블보기의 배경보기는 테이블보기의 크기에 맞게 자동으로 크기가 조정됩니다. 이보기는 모든 셀, 머리글보기 및 바닥 글보기 뒤에 테이블보기의 하위보기로 배치됩니다.

테이블 뷰의 배경색을 설정하려면이 속성을 nil로 설정해야합니다.


답변

Xcode 8.3.2-Swift 3.1 용

다음은 Xcode 7로 돌아가는 빈 테이블 뷰에 “No Items”뷰를 추가 할 수있는 아주 잘 알려지지 않았지만 믿을 수 없을 정도로 쉬운 방법입니다. 테이블의 배경보기를 볼 수 있지만 다음은 Xcode (8.3.2) 스토리 보드의 흐름입니다.

  1. 테이블보기가있는 스토리 보드에서 장면을 선택합니다.
  2. 빈 UIView를 해당 장면의 “Scene Dock”으로 드래그합니다.

여기에 이미지 설명 입력

  1. 새보기에 UILabel 및 제약 조건을 추가 한 다음 해당보기에 대한 IBOutlet을 만듭니다.

여기에 이미지 설명 입력

  1. 해당 뷰를 tableView.backgroundView에 할당하십시오.

여기에 이미지 설명 입력

  1. 마법을 보라!

여기에 이미지 설명 입력

궁극적으로 이것은 즉시 표시되고 싶지는 않지만 코드를 작성하고 싶지 않은 뷰 컨트롤러에 간단한 뷰를 추가하려는 경우 언제든지 작동합니다.


답변

위 코드의 신속한 버전 :-

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    var numOfSection: NSInteger = 0

    if CCompanyLogoImage.count > 0 {

        self.tableView.backgroundView = nil
        numOfSection = 1


    } else {

        var noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
        noDataLabel.text = "No Data Available"
        noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0)
        noDataLabel.textAlignment = NSTextAlignment.Center
        self.tableView.backgroundView = noDataLabel

    }
    return numOfSection
}

하지만 JSON에서 정보를로드하는 경우 JSON이 비어 있는지 확인해야하므로 이와 같은 코드를 입력하면 처음에는 “데이터 없음”메시지가 표시되고 사라집니다. 테이블이 데이터를 다시로드 한 후 메시지가 숨겨지기 때문입니다. 따라서 JSON 데이터를 배열로로드하는 곳에이 코드를 넣을 수 있습니다. 그래서 :-

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

func extract_json(data:NSData) {


    var error: NSError?

    let jsonData: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers , error: &error)
    if (error == nil) {
        if let jobs_list = jsonData as? NSArray
        {
            if jobs_list.count == 0 {

                var noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
                noDataLabel.text = "No Jobs Available"
                noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0)
                noDataLabel.textAlignment = NSTextAlignment.Center
                self.tableView.backgroundView = noDataLabel

            }

            for (var i = 0; i < jobs_list.count ; i++ )
            {
                if let jobs_obj = jobs_list[i] as? NSDictionary
                {
                    if let vacancy_title = jobs_obj["VacancyTitle"] as? String
                    {
                        CJobTitle.append(vacancy_title)

                        if let vacancy_job_type = jobs_obj["VacancyJobType"] as? String
                        {
                            CJobType.append(vacancy_job_type)

                            if let company_name = jobs_obj["EmployerCompanyName"] as? String
                            {
                                CCompany.append(company_name)

                                    if let company_logo_url = jobs_obj["EmployerCompanyLogo"] as? String
                                    {
                                        //CCompanyLogo.append("http://google.com" + company_logo_url)

                                        let url = NSURL(string: "http://google.com" + company_logo_url )
                                        let data = NSData(contentsOfURL:url!)
                                        if data != nil {
                                            CCompanyLogoImage.append(UIImage(data: data!)!)
                                        }

                                        if let vacancy_id = jobs_obj["VacancyID"] as? String
                                        {
                                            CVacancyId.append(vacancy_id)

                                        }

                                    }

                            }

                        }
                    }
                }
            }
        }
    }
    do_table_refresh();


}

func do_table_refresh() {

    dispatch_async(dispatch_get_main_queue(), {
        self.tableView.reloadData()
        return
    })
}


답변

이 컨트롤을 사용해 볼 수 있습니다. 꽤 깔끔합니다. DZNEmptyDataSet

아니면 내가 너라면 내가 할 건

  • 데이터 배열이 비어 있는지 확인하십시오.
  • 비어 있으면 @ “No Data”라는 하나의 개체를 추가합니다.
  • cell.textLabel.text에 해당 문자열 표시

쉬워요


답변

Swift 3 (업데이트 됨) :

override func numberOfSections(in tableView: UITableView) -> Int {
    if myArray.count > 0 {
        self.tableView.backgroundView = nil
        self.tableView.separatorStyle = .singleLine
        return 1
    }

    let rect = CGRect(x: 0,
                      y: 0,
                      width: self.tableView.bounds.size.width,
                      height: self.tableView.bounds.size.height)
    let noDataLabel: UILabel = UILabel(frame: rect)

    noDataLabel.text = "Custom message."
    noDataLabel.textColor = UIColor.white
    noDataLabel.textAlignment = NSTextAlignment.center
    self.tableView.backgroundView = noDataLabel
    self.tableView.separatorStyle = .none

    return 0
}


답변

스위프트 3.0

나는 그것이 당신의 목적을 서버로 삼기를 바랍니다 … 당신의 UITableViewController.

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchController.isActive && searchController.searchBar.text != "" {
        if filteredContacts.count > 0 {
            self.tableView.backgroundView = .none;
            return filteredContacts.count
        } else {
            Helper.EmptyMessage(message: ConstantMap.NO_CONTACT_FOUND, viewController: self)
            return 0
        }
    } else {
        if contacts.count > 0 {
            self.tableView.backgroundView = .none;
            return contacts.count
        } else {
            Helper.EmptyMessage(message: ConstantMap.NO_CONTACT_FOUND, viewController: self)
            return 0
        }
    }
}

함수가있는 도우미 클래스 :

 /* Description: This function generate alert dialog for empty message by passing message and
           associated viewcontroller for that function
           - Parameters:
            - message: message that require for  empty alert message
            - viewController: selected viewcontroller at that time
         */
        static func EmptyMessage(message:String, viewController:UITableViewController) {
            let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: viewController.view.bounds.size.width, height: viewController.view.bounds.size.height))
            messageLabel.text = message
            let bubbleColor = UIColor(red: CGFloat(57)/255, green: CGFloat(81)/255, blue: CGFloat(104)/255, alpha :1)

            messageLabel.textColor = bubbleColor
            messageLabel.numberOfLines = 0;
            messageLabel.textAlignment = .center;
            messageLabel.font = UIFont(name: "TrebuchetMS", size: 18)
            messageLabel.sizeToFit()

            viewController.tableView.backgroundView = messageLabel;
            viewController.tableView.separatorStyle = .none;
        }


답변

나는 당신의 문제를 해결하는 가장 우아한 방법은로 전환 생각 UITableViewControllerA를 UIViewController가 포함 UITableView. 이렇게 UIView하면 원하는 것을 기본보기의 하위보기로 추가 할 수 있습니다 .

이 작업을 수행하기 위해 UITableViewCell을 사용하는 것은 권장하지 않습니다. 앞으로 추가해야 할 사항이있을 수 있으며 상황이 빠르게 추악해질 수 있습니다.

이와 같은 작업을 수행 할 수도 있지만 이것이 최상의 솔루션은 아닙니다.

UIWindow* window = [[UIApplication sharedApplication] keyWindow];
[window addSubview: OverlayView];