Option Explicit
Public Sub ReplaceLinks()
Dim linksArr()
Application.ScreenUpdating = False
Dim myRange As Range
Set myRange = Application.InputBox("Please select both columns containing range of hyperlinks to update", Type:=8)
If Not myRange Is Nothing And myRange.Columns.Count = 2 Then
linksArr = myRange.Value
Else
MsgBox "Please select a range of two columns"
Exit Sub
End If
ReDim Preserve linksArr(1 To UBound(linksArr), 1 To 3)
linksArr = ValidateUrls(linksArr)
Dim currentLink As Long
For currentLink = LBound(linksArr, 1) To UBound(linksArr, 1)
If linksArr(currentLink, 3) Then
UpdateMyHyperlink CStr(linksArr(currentLink, 1)), CStr(linksArr(currentLink, 2))
End If
Next currentLink
WriteValidationResults linksArr, myRange
End Sub
Private Function ValidateUrls(ByVal linksArr As Variant) As Variant
Dim currentLink As Long
For currentLink = LBound(linksArr, 1) To UBound(linksArr, 1)
linksArr(currentLink, 3) = IsURLGood(CStr(linksArr(currentLink, 1)))
Next currentLink
ValidateUrls = linksArr
End Function
Public Function IsURLGood(ByVal url As String) As Boolean
'https://www.experts-exchange.com/questions/27240517/vba-check-URL-if-it-is-active-or-not.html by m4trix
Dim request As WinHttpRequest
Set request = New WinHttpRequest
On Error GoTo IsURLGoodError
request.Open "HEAD", url
request.Send
IsURLGood = request.Status = 200
Exit Function
IsURLGoodError:
IsURLGood = False
End Function
Private Sub UpdateMyHyperlink(ByVal oldUrl As String, ByVal newUrl As String)
Dim ws As Variant
Dim hyperlink As Variant
For Each ws In ThisWorkbook.Worksheets
For Each hyperlink In ws.Hyperlinks
If hyperlink.Address = oldUrl & "/" Then
hyperlink.Address = Application.WorksheetFunction.Substitute(hyperlink.Address, oldUrl, newUrl)
hyperlink.TextToDisplay = newUrl
End If
Next
Next
End Sub
Private Sub WriteValidationResults(ByVal linksArr As Variant, ByRef myRange As Range)
Dim isUrlValidOutput As Range
Set isUrlValidOutput = myRange.Offset(, 2).Resize(myRange.Rows.Count, 1)
isUrlValidOutput = Application.Index(linksArr, , 3)
isUrlValidOutput.Offset(-1, 0).Resize(1) = "Valid URL"
End Sub
上面是我看到的代码。
我的代码如下
我不知道为什么它的价值为零 tableView.dequeueReusableCell(withIdentifier:" locCell")
我的故事板如下
我在下面添加了标识符(你可以在pic的右下方看到它
答案 0 :(得分:1)
您需要注册单元格以便重复使用。
tableView.register(LocationTableCell.self, forCellReuseIdentifier: "locCell")
或者在故事板中输入您的重用标识符,方法是选择您的单元格,然后在右侧的属性中输入重用标识符。
答案 1 :(得分:1)
只是因为tableView.dequeueReusableCell(withIdentifier: "cell")
默认为nil
。
尝试打印时,任何可选的情况都是一样的,例如:
let optionalString: String? = ""
print(optionalString)
导致获得:
因此,通过将常量声明为:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
由于dequeueReusableCell(withIdentifier:)
会返回可选的UITableViewCell
个实例,cell
的类型将为UITableViewCell?
(可选UITableViewCell
),这就是您的原因正在看到这个错误。
如何摆脱它?
假设您已为细胞设置了细胞正确的标识符:
好吧,如果有自定义单元格,可以将其转换为:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? MyCustomCell else {
// something goes wrong
return UITableViewCell()
}
print(cell) // it would be fine for now
// ...
return cell
}
如果你没有自定义单元格,那么你所要做的就是删除as? MyCustomCell
向下投射。
答案 2 :(得分:0)
替换
let cell = tableView.dequeueReusableCell(withIdentifier: "locCell")
使用此代码:
let cell = tableView.dequeueReusableCell(withIdentifier: "locCell", for: indexPath)
答案 3 :(得分:0)
func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier", for: indexPath) as! CustomTableViewCell
cell.label.text = data[indexPath.row].name
return cell
}
注意:在故事板中设置tableView Delegate,DataSource和set cell ID cellReuseIdentifier。