No Results - UITableView iPhone



当表视图为空时,我试图在表视图上显示无结果消息。我已经使用了uilabel方法,当它为空时会出现,但这似乎不是苹果在联系人等中的做法,当你试图上下滚动时,"无结果"也会移动。我的只是停留在一个地方。

有人知道怎么做吗?

我想他们添加了一个"无结果"单元格?

是。如果没有结果显示,请执行以下

  1. 创建一个名为noResultsToDisplay或其他名称的布尔标志
  2. 如果没有要显示的结果,则设置noResultsToDisplay = YES,否则设置为NO
  3. numberOfRowsInSection中,if (noResultsToDisplay) return 3;
  4. cellForRowAtIndexPath中,if (noResultsToDisplay && indexPath.row == 2) cell.textLabel.text = @"No Results";
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 3;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell...
    if (indexPath.row == 2) {
        cell.textLabel.text = @"Empty cell";
    }
    return cell;
}

有两种方法:按照自己的建议执行,创建一个"无结果单元格",并在tableViewController中设置BOOL resultIsEmpty=YES状态。在单元格ForRowAtIndexPath中,您首先测试这个空BOOL,并且只返回无结果单元格,请记住还检查numberOfRowsInSection,这样在空的情况下可以返回1(否则它可能会返回模型数组的长度,当然是0)。

另一种方法是在表视图上做一个y插入,并将标签放在那里。之所以能够做到这一点,是因为UITableView是UIScrollView的一个子类。

self.tableView.contentInset = UIEdgeInsetsMake(heighOfNoResultLabel, 0, 0, 0);

然后在"resultsDidLoad"或新数据的委托的名称中,测试它是否为0,并插入tableView并在那里放置标签。如果不是0,则将插图设置为

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

所有0。您可以设置此属性的动画,以便在没有结果时,表视图向下"滚动"以显示"无结果"标签。

Bot解决方案是有效的,我想说,大约相同数量的代码。区别可能在于你以后可以用它做什么。

我已经解决了这个问题,我维护了一个与表具有相同farme的标签视图,并使用标签和表的"隐藏"属性(始终一个为YES,另一个为NO)。

i have edited the accepted answer to be look like the no search results in tableView

    Create a boolean flag named noResultsToDisplay, or something else.
    1-If you have no results to display then set noResultsToDisplay = YES, set it to NO otherwise.
    2-(this step changed)In numberOfRowsInSection, if (noResultsToDisplay) return 1; 
// 1 returned not 3
   3-(this step changed) In cellForRowAtIndexPath, 
static NSString *CellIdentifier;
    if (noResultsToDisplay){
        CellIdentifier = @"Cell";
// in my case i use custom cell this step can be skipped if you use the default UITableViewCell
    }
    else{
       CellIdentifier = @"DocInqueryCell";
    }
 DocumentationInqueryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// don't forgt to add cell in your storyboard with identifier Cell and class your custom class , again this step can be skipped if you use the default cell
then 
if (noResultsToDisplay) {
cell.textLabel.text = @"No Results";
}else{
do what you want in your custom cell
}

最新更新