SearchController-我不希望用户未开始输入时显示数据

时间:2018-09-24 18:23:00

标签: swift xcode firebase

]我已经在我的应用程序中构建了SearchViewController,它只是搜索用户。

一切正常,除了当我进入viewcontroller时,它会显示我甚至还没有开始搜索之前firebase数据库中的每个用户?我希望tableview在输入至少2个字母之前不显示结果。有人知道我可以实现吗?

这是我的代码:

Baa

那么,有人对我如何进行这项工作有任何提示吗? this is the error im getting

2 个答案:

答案 0 :(得分:1)

在您的numberOfRowsInSection中,您将返回self.usersArray.count。如果不显示所有结果,则只需返回0,就不会显示任何结果。

如果要在输入至少两个字符后显示搜索,请使用searchBar.text的count属性。

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if searchController.isActive && searchController.searchBar.text != "" && searchController.searchBar.text.count >= 2 {
        return filteredUsers.count
    }

    return 0

}

答案 1 :(得分:0)

使用您的原始代码:

   library(h2o)
h2o.init()
# import the titanic dataset
df <- h2o.importFile(path = "http://s3.amazonaws.com/h2o-public-test-data/smalldata/gbm_test/titanic.csv")
dim(df)
head(df)
tail(df)
summary(df,exact_quantiles=TRUE)

# pick a response for the supervised problem
response <- "survived"

# the response variable is an integer.
# we will turn it into a categorical/factor for binary classification
df[[response]] <- as.factor(df[[response]])

# use all other columns (except for the name) as predictors
predictors <- setdiff(names(df), c(response, "name"))

# split the data for machine learning
splits <- h2o.splitFrame(data = df,
                         ratios = c(0.6,0.2),
                         destination_frames = c("train.hex", "valid.hex", "test.hex"),
                         seed = 1234)
train <- splits[[1]]
valid <- splits[[2]]
test  <- splits[[3]]

# Establish a baseline performance using a default GBM model trained on the 60% training split
# We only provide the required parameters, everything else is default
gbm <- h2o.gbm(x = predictors, y = response, training_frame = train)

# Get the AUC on the validation set
h2o.auc(h2o.performance(gbm, newdata = valid))
# The AUC is over 94%, so this model is highly predictive!
[1] 0.9480135

# Determine the best max_depth value to use during a hyper-parameter search.
# Depth 10 is usually plenty of depth for most datasets, but you never know
hyper_params = list( max_depth = seq(1,29,2) )
# or hyper_params = list( max_depth = c(4,6,8,12,16,20) ), which is faster for larger datasets

grid <- h2o.grid(
  hyper_params = hyper_params,

  # full Cartesian hyper-parameter search
  search_criteria = list(strategy = "Cartesian"),

  # which algorithm to run
  algorithm="gbm",

  # identifier for the grid, to later retrieve it
  grid_id="depth_grid",

  # standard model parameters
  x = predictors,
  y = response,
  training_frame = train,
  validation_frame = valid,

  # more trees is better if the learning rate is small enough
  # here, use "more than enough" trees - we have early stopping
  ntrees = 10000,

  # smaller learning rate is better, but because we have learning_rate_annealing,
  # we can afford to start with a bigger learning rate
  learn_rate = 0.05,

  # learning rate annealing: learning_rate shrinks by 1% after every tree
  # (use 1.00 to disable, but then lower the learning_rate)
  learn_rate_annealing = 0.99,

  # sample 80% of rows per tree
  sample_rate = 0.8,

  # sample 80% of columns per split
  col_sample_rate = 0.8,

  # fix a random number generator seed for reproducibility
  seed = 1234,

  # early stopping once the validation AUC doesn't improve by at least
  # 0.01% for 5 consecutive scoring events
  stopping_rounds = 5,
  stopping_tolerance = 1e-4,
  stopping_metric = "AUC",

  # score every 10 trees to make early stopping reproducible
  # (it depends on the scoring interval)
  score_tree_interval = 10)

# by default, display the grid search results sorted by increasing logloss
# (because this is a classification task)
grid

# sort the grid models by decreasing AUC
sortedGrid <- h2o.getGrid("depth_grid", sort_by="auc", decreasing = TRUE)
sortedGrid

# find the range of max_depth for the top 5 models
topDepths = sortedGrid@summary_table$max_depth[1:5]
minDepth = min(as.numeric(topDepths))
maxDepth = max(as.numeric(topDepths))

> sortedGrid

您要返回override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows if searchController.isActive && searchController.searchBar.text != ""{ return filteredUsers.count } return self.usersArray.count } ,所以可能可以解释为什么它在开始时显示整个未过滤的数组,而没有在搜索栏中输入任何文本。

我还注意到您的self.userArray.count函数尚未在您的代码中调用。

但是我认为,filterContent(searchText:String)行可以用作默认返回值,当您不想更改结果表时,因为当表为空,有1或3时,它可能会移向用户已经输入了2个字母。

在代码的另一部分:

return self.usersArray.count

使用self.tableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic)时,我不再需要self.tableView.reloadData()

关于崩溃/错误:

在您的代码块中:

insertRow

代替 if(key == self.loggedInUser?.uid) { print("Same as logged in user, so don't show!") } else { self.usersArray.append(snapshot) //insert the rows self.tableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic) self.tableView.reloadData() }

请尝试:

self.tableView.reloadData()

这是为了明确让tableviewcontroller知道您正在开始编辑/更改内容。请注意,使用 if(key == self.loggedInUser?.uid) { print("Same as logged in user, so don't show!") } else { self.usersArray.append(snapshot) //insert the rows self.tableView.beginUpdates() self.tableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic) self.tableView.endUpdates() } 总是与beginUpdates()配对,并且不应与endUpdates()一起使用。