如果我只是想通过代码设置标签,我应该写这样的例子,
let label = UILabel()
label.frame = CGRect(x:10, y: 10, width:160, height:30)
label.text = "Test"
self.view.addSubview(label)
但如果我想在tableView的单元格中设置标签,我该如何设置呢?
谢谢!
答案 0 :(得分:0)
UITableviewCell子类具有textlabel属性,您可以使用
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
let label = UILabel()
label.frame = CGRect(x: 10, y: 10, width: 160, height: 30)
label.text = "Test"
cell.contentView.addSubview(label)
return cell
}
或者您可以使用自定义单元格进行更多控制
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, :except => [:notify]
skip_before_action :verify_authenticity_token
protect_from_forgery :except => [:notify]
def sales
@orders = Order.all.where(seller: current_user).order("created_at DESC")
end
def purchases
@orders = Order.all.where(buyer: current_user).order("created_at DESC")
end
# GET /orders/new
def new
@order = Order.new
@item = Item.find(params[:item_id])
end
# POST /orders
# POST /orders.json
def create
@@order = Order.new(order_params)
@item = Item.find(params[:item_id])
@seller = @item.user
@@order.item_id = @item.id
@@order.buyer_id = current_user.id
@@order.seller_id = @seller.id
# p = Print.find_by(id: params[:id])
# creator = Creator.find_by(id: p.creator_id)
price = (@item.price + @item.shipping_price.to_i)
commission = 0.06
# Build API call
@api = PayPal::SDK::AdaptivePayments.new
@pay = @api.build_pay({
:actionType => "PAY",
:cancelUrl => "http://f4bd4637.ngrok.io" + new_item_order_path,
:returnUrl => root_url,
:currencyCode => "USD",
:feesPayer => "PRIMARYRECEIVER",
:ipnNotificationUrl => "http://f4bd4637.ngrok.io" + paypal_ipn_notify_path,
:receiverList => {
:receiver => [
{
:amount => price,
:email => @@order.item.paypal_email,
:primary => true
},
{
:amount => price * commission,
:email => "example@gmail.com",
:primary => false
}
]
}})
# Make API call
@pay_response = @api.pay(@pay)
# Check if call was valid, if so, redirect to PayPal payment url
if @pay_response.success?
@pay_response.payKey
redirect_to @api.payment_url(@pay_response)
else
redirect_to root_path, alert: @pay_response.error[0].message
end
end
def notify
response = validate_IPN_notification(request.raw_post)
case response
when "VERIFIED"
@@order.save
when "INVALID"
else
end
render :nothing => true
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
@order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
if params[:orders] && params[:orders][:stripe_card_token].present?
params.require(:orders).permit(:stripe_card_token)
end
end
protected
def validate_IPN_notification(raw)
uri = URI.parse('https://ipnpb.sandbox.paypal.com/cgi-bin/webscr?cmd=_notify-validate')
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 60
http.read_timeout = 60
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
response = http.post(uri.request_uri, raw,
'Content-Length' => "#{raw.size}",
'User-Agent' => "My custom user agent"
).body
end
end
答案 1 :(得分:0)
您应该为您创建一个自定义类。这会向单元格添加标签,并使用锚定系统在标签上设置约束以填充整个单元格。
class CustomCell: UITableViewCell {
let label: UILabel = {
let n = UILabel()
n.textColor = UIColor.darkGray
n.textAlignment = .center
n.text = "Testing 123"
n.font = UIFont(name: "Montserrat", size: 30)
return n
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
label.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
label.topAnchor.constraint(equalTo: topAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
对于使用此自定义单元格的ViewController,您必须添加以下单元格注册,除非您使用的是故事板/界面构建器。
class ControllerUsesCell: UITableViewController {
let defaultCellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
tableView?.register(CustomCell.self, forCellWithReuseIdentifier: defaultCellId)
}
}