表为空时,iPhone UITableView图像背景

时间:2012-01-18 21:24:55

标签: iphone objective-c uitableview background

我想在UITableView为空时显示图像背景。 目前我尝试将UIImageView添加到包含表的视图控制器,但XCode不允许它。

有没有好办法呢?

3 个答案:

答案 0 :(得分:15)

您可以在表格视图的顶部添加图像视图,也可以更改表格视图的背景视图。

// Check if table view has any cells
int sections = [self.tableView numberOfSections];
BOOL hasRows = NO;
for (int i = 0; i < sections; i++) {
    BOOL sectionHasRows = ([self.tableView numberOfRowsInSection:i] > 0) ? YES : NO;
    if (sectionHasRows) {
        hasRows = YES;
        break;
    }
}

if (sections == 0 || hasRows == NO)
{
    UIImage *image = [UIImage imageNamed:@"test.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    // Add image view on top of table view
    [self.tableView addSubview:imageView];

    // Set the background view of the table view
    self.tableView.backgroundView = imageView;
}

答案 1 :(得分:4)

UITableView具有backgroundView属性。将此属性设置为包含背景图片的UIImageView

答案 2 :(得分:0)

numberOfRowsInSection 方法中:

    <!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      var map;
      var score;
      score = 0;
      function initMap() {
        var chicago = {lat: 41.8781, lng: -87.6298};
        var indianapolis = {lat: 39.7684, lng: -86.1581};
        var oklahomaCity = {lat: 35.4819, lng: -97.5084};
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: 0.0, lng: 0.0},
          zoom: 1
});
        var chicagoMarker = new google.maps.Marker({
            position: chicago,  


});

        var oklahomaCityMarker = new google.maps.Marker({
            position: oklahomaCity,
});

        var indianapolisMarker = new google.maps.Marker({
            position: indianapolis,

});

if (score==0) {
    chicagoMarker.setMap(map);
    map.getZoom();
    google.maps.event.addListener(chicagoMarker,'bounds_changed',function(){
        if (map.getBounds().contains(chicagoMarker)){
            alert("Good Job");
        }
        if (zoom == 8 && map.getBounds.contains(chicagoMarker)){
            window.alert("You have found city 1!");
            score = score + 1;
        }
    })

};

if (score==1) {
    oklahomaCityMarker.setMap(map)
}

if (score==2) {
    indianapolisMarker.setMap(map)
}


chicagoMarker.setVisible(false);
indianapolisMarker.setVisible(false);
oklahomaCityMarker.setVisible(false);
    }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCuvsCAF0gVmwv6AF0SoA3xBjV66RG4r7o&callback=initMap"
    async defer></script>
  </body>
</html>

numberOfSections 应大于0