UITableview reusable cell issue swift - objective-c

I have tableview in which each cell contains label,image and button.
My problem is when i disabled first cell and scroll table view 5th cell also disable
here is code
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! DetailTableViewCell
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
print(indexPath.row)
if indexPath.row == videoName.count
{
cell.video_name?.hidden = true
cell.artist_name?.hidden = true
cell.img?.hidden = true
cell.viewer?.hidden = true
cell.btn_add?.hidden = true
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
indicator.center = cell.contentView.center
if self.pageToken != ""
{
cell.contentView.addSubview(indicator)
indicator.startAnimating()
if videoIDArr.count == 25 || videoIDArr.count == 5
{
if check == 0
{
loadMoreMostPopularVideo()
}
else
{
loadMorePlaylistVideo()
}
}
}
}
else
{
indicator.stopAnimating()
cell.video_name?.hidden = false
cell.artist_name?.hidden = false
cell.img?.hidden = false
cell.viewer?.hidden = false
cell.btn_add?.hidden = false
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
cell.video_name?.text = videoName[indexPath.row]
cell.artist_name?.text = artistName[indexPath.row]
cell.img?.sd_setImageWithURL(NSURL(string: self.thumbURL[indexPath.row]))
cell.viewer?.text = "\(viewerArr[indexPath.row]) views"
cell.btn_add.tag = indexPath.row
cell.btn_add?.addTarget(self, action: "addVideo:", forControlEvents: UIControlEvents.TouchUpInside)
// Disable cell code
if dataVideoName.contains(cell.video_name.text!)
{
cell.img.alpha = 0.50
cell.btn_add.alpha = 0.50
cell.viewer.alpha = 0.50
cell.video_name.alpha = 0.50
cell.artist_name.alpha = 0.50
cell.userInteractionEnabled = false
}
}
return cell
}
I have face this problem in morning
give me some suggestion to solve this problem
Thanks

You need enable cell codes for if indexPath.row == videoName.count {} to have cell's settings symmetrically
if indexPath.row == videoName.count {
....
// Enable cell code
if dataVideoName.contains(cell.video_name.text!)
{
cell.img.alpha = 1
cell.btn_add.alpha = 1
cell.viewer.alpha = 1
cell.video_name.alpha = 1
cell.artist_name.alpha = 1
cell.userInteractionEnabled = true
}
} else {
....
// Disable cell code
if dataVideoName.contains(cell.video_name.text!)
{
cell.img.alpha = 0.50
cell.btn_add.alpha = 0.50
cell.viewer.alpha = 0.50
cell.video_name.alpha = 0.50
cell.artist_name.alpha = 0.50
cell.userInteractionEnabled = false
}
}

Related

I want to create sticky header for particular section in UICollectionView in Swift

I used collection view with 3 different sections where 1st section shows banner, 2nd section shows the sub categories and the 3rd shows the items/products, now what I want is when I scroll the whole view the 2nd section should work as a sticky header on scrolling. 
I have done all the possible things that can be done, but nowhere near to solve the issue
Kindly help me out
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch section {
case 0: return 1
case 1: return subCategories.count
case 2: return selectedSubCategoryID == -1 ? products.count : productsSub.count
default: return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch indexPath.section {
case 0:
let cell = collectionView.dequeueReusableCell(ofType: ShopBannerCell.self, for: indexPath)
cell.shopLogoImageView.loadImage(shop.logo)
cell.shopLogoImageView.layer.borderColor = UIColor.lightGray.cgColor
cell.shopLogoImageView.layer.borderWidth = 1
cell.shopBannerImageView.loadImage(details?.sliders?.first)
cell.images = details?.sliders ?? []
cell.shopNameLabel.text = shop.name
// cell.lcoationButton.setTitle(details?.address, for: .normal)
cell.showSideMenu = { [weak self] sender in
if let navVC = self?.navigationController as? NavigationController {
navVC.showSideMenu()
}
}
if currentLanguage == "en" {
cell.backArrowBtn.setImage(#imageLiteral(resourceName: "left_arrow"), for: .normal)
} else
{
cell.backArrowBtn.setImage(#imageLiteral(resourceName: "right-arrow"), for: .normal)
}
cell.backButton = { [weak self] sender in
self?.navigationController?.popViewController(animated: true)
}
cell.showSearch = { [weak self] sender in
NotificationCenter.default.post(name: .navigateToShopVC, object: self)
}
cell.showShopVC = { [weak self] sender in
self?.navigationController?.popToRootViewController(animated: true)
}
cell.showFilterVC = { [weak self] sender in
let productFilterVC = ProductFilterVC()
productFilterVC.modalPresentationStyle = .overFullScreen
productFilterVC.showShopVC = { [weak self] in
self?.navigationController?
.popToRootViewController(animated: true)
}
// variant_product
productFilterVC.applyFilters = { [weak self] filter in
if let priceRange = filter.priceRange, !priceRange.isEmpty, priceRange != 0...0 {
if self?.selectedSubCategoryID == -1 {
self?.products = self?.products.filter { priceRange.contains(Int($0.basePrice ?? 0))} ?? []
} else {
self?.productsSub = self?.productsSub.filter { priceRange.contains(Int($0.unitPrice ?? "0") ?? 0)} ?? []
}
}
if filter.sortOrder != -1 {
if self?.selectedSubCategoryID == -1 {
self?.products.sort(by: { item1, item2 -> Bool in
switch filter.sortOrder {
case 0: return item1.basePrice ?? 0 < item2.basePrice ?? 0
case 1: return item1.basePrice ?? 0 > item2.basePrice ?? 0
case 2: return item1.discount ?? 0 < item2.discount ?? 0
default: return true
}
})
} else {
self?.productsSub.sort(by: { item1, item2 -> Bool in
switch filter.sortOrder {
case 0: return Double(item1.unitPrice ?? "") ?? 0 < Double(item2.unitPrice ?? "") ?? 0
case 1: return Double(item1.unitPrice ?? "") ?? 0 > Double(item2.unitPrice ?? "") ?? 0
case 2: return Double(item1.discount ?? "") ?? 0 < Double(item2.discount ?? "") ?? 0
default: return true
}
})
}
}
self?.collectionView.reloadSections(IndexSet(integer: 2))
}
self?.present(productFilterVC, animated: true)
}
cell.gridButton.isSelected = self.selectedLayout == .grid
cell.listButton.isSelected = self.selectedLayout == .list
cell.listlayout = { [weak self] sender in
self?.selectedLayout = .list
self?.collectionView.collectionViewLayout.invalidateLayout()
self?.collectionView.reloadData()
}
cell.gridLayout = { [weak self] sender in
self?.selectedLayout = .grid
self?.collectionView.collectionViewLayout.invalidateLayout()
self?.collectionView.reloadData()
}
return cell
case 1:
let cell = collectionView.dequeueReusableCell(ofType: SubCategoryCell.self, for: indexPath)
let subCategory = subCategories[indexPath.row]
if subCategory.id == selectedSubCategoryID {
cell.button.isSelected = true
cell.button.backgroundColor = .accentColor
} else {
cell.button.isSelected = false
cell.button.backgroundColor = .clear
}
if currentLanguage == "en" {
cell.button.setTitle(subCategory.name?.capitalized, for: .normal)
} else
{
cell.button.setTitle(subCategory.ar_name?.capitalized, for: .normal)
}
cell.onPress = { [weak self] sender in
guard let self = self else { return }
self.selectedSubCategoryID = self.subCategories[indexPath.row].id ?? 1
}
return cell
case 2:
if selectedLayout == .grid {
let cell = collectionView.dequeueReusableCell(ofType: ProductCell.self, for: indexPath)
// cell.addToCartButton.setTitle("Add to Cart".localized(), for: .normal)
let image = selectedSubCategoryID == -1 ? products[indexPath.row].photos.first : productsSub[indexPath.row].photos?.first
cell.productImageView.loadImage(image as? String)
if currentLanguage == "en"{
cell.productNameLabel.text = selectedSubCategoryID == -1 ? products[indexPath.row].name : productsSub[indexPath.row].name
} else {
cell.productNameLabel.text = selectedSubCategoryID == -1 ? products[indexPath.row].arName : productsSub[indexPath.row].arName
}
cell.pricingLabel.text = "KD ".localized() + "\(products[indexPath.row].baseDiscountedPrice ?? "")"
if products[indexPath.row].basePrice == Double(products[indexPath.row].baseDiscountedPrice ?? ""){
cell.discountView.isHidden = true
cell.pricingLabel.textAlignment = .center
cell.pricingLabel.textColor = UIColor.black
}
else
{
cell.discountView.isHidden = false
cell.discountedLBL.text = "KD " + (String(products[indexPath.row].basePrice ?? 0)) + "0"
cell.pricingLabel.textColor = UIColor.red
}
// let price = selectedSubCategoryID == -1 ? "\(products[indexPath.row].basePrice ?? 0)" : productsSub[indexPath.row].unitPrice ?? ""
return cell
} else {
let cell = collectionView.dequeueReusableCell(ofType: ProductListCell.self, for: indexPath)
// cell.addToCartButton.setTitle("Add to Cart".localized(), for: .normal)
let image = selectedSubCategoryID == -1 ? products[indexPath.row].photos.first : productsSub[indexPath.row].photos?.first
cell.productImageView.loadImage(image as? String)
cell.productImageView.layer.borderColor = UIColor.lightGray.cgColor
cell.productImageView.layer.borderWidth = 1
cell.pricingLabel.text = "KD ".localized() + "\(products[indexPath.row].baseDiscountedPrice ?? "")"
if products[indexPath.row].basePrice == Double(products[indexPath.row].baseDiscountedPrice ?? ""){
cell.discountView.isHidden = true
cell.pricingLabel.textAlignment = .center
cell.pricingLabel.textColor = UIColor.black
}
else
{
cell.discountView.isHidden = false
cell.discountLbl.text = "KD " + (String(products[indexPath.row].basePrice ?? 0)) + "0"
cell.pricingLabel.textColor = UIColor.red
}
if currentLanguage == "en"{
cell.productNameLabel.text = selectedSubCategoryID == -1 ? products[indexPath.row].name : productsSub[indexPath.row].name
} else {
cell.productNameLabel.text = selectedSubCategoryID == -1 ? products[indexPath.row].arName : productsSub[indexPath.row].arName
}
// let price = selectedSubCategoryID == -1 ? "\(products[indexPath.row].basePrice ?? 0)" : productsSub[indexPath.row].unitPrice ?? ""
let productID = selectedSubCategoryID == -1 ? "\(products[indexPath.row].id ?? 0)" : "\(productsSub[indexPath.row].id ?? "")"
cell.onAddToCart = { [weak self] _ in
if UserRepository.shared.userName == "GUEST" {
self?.promptForGuestUser()
return
}
self?.addToCartAPI(product: "\(productID)")
}
return cell
}
default:
return collectionView.dequeueReusableCell(ofType: SubCategoryCell.self, for: indexPath)
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
switch indexPath.section {
case 2:
let product = products[indexPath.row]
let vc = ProductDetailsVC(id: "\(product.id ?? 0)",
subCategoryID: product.subcategoryID ?? "",
sellerID: product.sellerID ?? "")
navigationController?.pushViewController(vc, animated: true)
default: break
}
}

GML - Not passing through a part of the code

This is a Trigger Warp code:
if (needEnter) {
if (keyboard_check(vk_enter)) {
audio_play_sound(door_open,0,0);
room_goto(targetRoom);
obj_player.x = targetX;
obj_player.y = targetY;
obj_player.image_index = playerSide
}
else {
if (!instance_exists(obj_msgballoon)) {
balloon = instance_create_layer(obj_player.x, obj_player.bbox_top, "hud", obj_msgballoon);
balloon.balloon_msg = msg_openable_door;
}
playerIsIn = true
}
}
else {
room_goto(targetRoom);
obj_player.x = targetX;
obj_player.y = targetY;
}
The code above triggers the spawn of the obj_msgballoon Instance normally... but something like this doesn't:
if (keyboard_check(vk_enter)) {
if instance_exists(obj_msgballoon) {
if (obj_msgballoon.image_yscale > 1) {
if cooldown < 1 {
instance_destroy(obj_msgballoon)
balloon = instance_create_layer(obj_player.x, obj_player.bbox_top, "hud", obj_msgballoon)
balloon.isTimed = true;
balloon.balloon_msg = msg_locked_door;
cooldown = 15;
}
}
cooldown--;
}
else {
balloon = instance_create_layer(obj_player.x, obj_player.bbox_top, "hud", obj_msgballoon)
balloon.isTimed = true;
balloon.balloon_msg = msg_locked_door;
}
if (!audio_is_playing(locked_door_snd)) {
audio_play_sound(locked_door_snd,0,0);
}
}
Even if I do something like this it does not work:
balloon = instance_create_layer(obj_player.x, obj_player.bbox_top, "hud", obj_msgballoon);
balloon.balloon_msg = msg_openable_door;
I just can't find out the problem... maybe I'm missing something?
Edit: Here it is the code of the obj_msgballoon;
STEP:
if (p) {
sprite_index = ballon_msg;
p = false;
}
if (!isTimed) {
if image_yscale < 1 {
image_yscale += 0.3
}
x = obj_player.x
y = obj_player.bbox_top - 15
} else {
if image_yscale < 1 {
image_yscale += 0.3
}
x = obj_player.x
y = obj_player.bbox_top - 15
if cooldown < 1 {
instance_destroy();
}
cooldown--;
}
CREATE:
image_yscale = 0
p = true;
cooldown = 40;
isTimed = false;
balloon_msg = pointer_null;

How do you fix Fatal error: index out of range? Swift

I know I have to return some sort of value in my code so it does not keep crashing with this thread error, but I can't figure it out. Please help! Here is the code- `
#IBAction func decideBttn(_ sender: Any) {
// if there is data in more than one Label randomly pick 1 out of 2
if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false
{
var topics = [valueLbl1.text!, valueLbl2.text!]
pickTopic = Int(arc4random_uniform(UInt32(topics.count)))
topics.remove(at: pickTopic)
resultLbl.text = "\(topics[pickTopic])" //Where fatal error occurs
valueLbl1.text = ""
valueLbl2.text = ""
valueLbl3.text = ""
return
}
// if all 3 Labels are used button will randomly pick 1 out of 3
else if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false && valueLbl3.text?.isEmpty == false
{
var topics = [ valueLbl1.text!, valueLbl2.text!, valueLbl3.text!]
pickTopic = Int(arc4random_uniform(UInt32(topics.count)))
topics.remove(at: pickTopic)
resultLbl.text = "\(topics[pickTopic])"
// resetting variable value
valueLbl1.text = ""
valueLbl2.text = ""
valueLbl3.text = ""
return
}

Hyperloop module LFLiveKit

I have an issue with native issues in my hyperloop module for LFLiveKit, looking for some advice and help.
I only see a red screen showing.
I also get the alerts for the permissions.
Here is my code:
var UIScreen = require('UIKit/UIScreen'),
UIViewController = require('UIKit/UIViewController'),
UIView = require('UIKit/UIView'),
UIColor = require('UIKit/UIColor'),
CGPointMake = require('CoreGraphics').CGPointMake,
CGRectMake = require('CoreGraphics').CGRectMake,
NSBundle = require('Foundation/NSBundle'),
NSURL = require('Foundation/NSURL'),
NSData = require('Foundation/NSData'),
AVPlayer = require('AVFoundation/AVPlayer'),
AVPlayerLayer = require('AVFoundation/AVPlayerLayer'),
UIColor = require('UIKit/UIColor'),
NSString = require("Foundation/NSString"),
UIImage = require('UIKit/UIImage'),
UIImageView = require('UIKit/UIImageView'),
LFLiveKit = require('LFLiveKit/LFLiveKit'),
LFLiveAudioConfiguration = require("LFLiveKit/LFLiveAudioConfiguration"),
LFLiveVideoConfiguration = require("LFLiveKit/LFLiveVideoConfiguration"),
LFLiveSession = require("LFLiveKit/LFLiveSession"),
LFLiveStreamInfo = require("LFLiveKit/LFLiveStreamInfo"),
LFLiveVideoQuality = require("LFLiveKit").LFLiveVideoQuality,
AVCaptureDevice = require("AVFoundation/AVCaptureDevice"),
AVMediaTypeVideo = require("AVFoundation").AVMediaTypeVideo,
AVMediaTypeAudio = require("AVFoundation").AVMediaTypeAudio,
TiApp = require('Titanium/TiApp');
var config = LFLiveAudioConfiguration.defaultConfiguration();
var audioConfiguration = LFLiveAudioConfiguration.defaultConfiguration();
var videoConfiguration = LFLiveVideoConfiguration.defaultConfiguration();
var viewController = UIViewController.alloc().init();
var session = LFLiveSession.alloc().init();
session.audioConfiguration = audioConfiguration;
session.videoConfiguration = videoConfiguration;
var bounds = UIScreen.mainScreen.bounds;
var frame = CGRectMake(0, 0, bounds.size.width, bounds.size.height);
function requestAccessForVideo() {
Ti.API.info("requestAccessForVideo");
var status = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo);
Ti.API.info(status);
}
Ti.Media.requestAudioRecorderPermissions(function(e) {
if (e.success) {
requestAccessForAudio();
alert('You dont denied permission');
} else {
alert('You denied permission');
}
});
function requestAccessForAudio() {
Ti.API.info("requestAccessForAudio");
var status = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeAudio);
Ti.API.info(status);
startLive();
}
requestAccessForVideo();
viewController.view.frame = frame;
viewController.view.backgroundColor = UIColor.redColor;
session.preView = viewController.view;
function startLive() {
session.running = true;
streamInfo = new LFLiveStreamInfo();
streamInfo.url = "rtmp://live.hkstv.hk.lxdns.com:1935/live/stream153";
session.startLive(streamInfo);
Ti.API.info("STARTED");
// TiApp.app().showModalController(viewController, true);
}
$.index.add(viewController.view);
$.index.open();

Collectionview cellForItemAtIndexPath not call after reload

i have 2 dictionary and dictionary contain array of passing in numberOfItemsInSection
1. self.dicMyClaimsSender
2. self.dicMyClaimsReceiver
I am using segment control. When I tap on segment, then I reload the collectionView (I am using 3 collection view).
// UISegmentedControl method
#IBAction func segmntAction(sender:UISegmentedControl) {
switch segmnetCtrl.selectedSegmentIndex {
case 0:
self.isSenderSelected = true
self.heightCollection.constant = 130.0
self.heightViewImg.constant = 0.0
self.heightViewVideo.constant = 0.0
self.heightViewFile.constant = 0.0
self.heightBtnAttach.constant = 42.0
self.heightBtnSendFile.constant = 42.0
self.collectionImg.reloadData()
self.collectionFile.reloadData()
self.collectionVideo.reloadData()
break
case 1:
self.isSenderSelected = false
self.heightViewImg.constant = 156.0
self.heightViewVideo.constant = 156.0
self.heightViewFile.constant = 156.0
self.heightCollection.constant = 0.0
self.heightBtnAttach.constant = 0.0
self.heightBtnSendFile.constant = 0.0
self.collectionImg.reloadData()
self.collectionFile.reloadData()
self.collectionVideo.reloadData()
break
default:
break;
}
}
// MARK: - UICollectionView Delegates & Datasources
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if self.isSenderSelected {
if collectionView.tag == 1 {
return self.assets?.count ?? 0
} else if collectionView.tag == 2 {
if self.dicMyClaimsSender.ary_Images.count == 0 {
self.heightViewImg.constant = 0.0
} else {
self.heightViewImg.constant = 156.0
}
return self.dicMyClaimsSender.ary_Images.count
} else if collectionView.tag == 3 {
if self.dicMyClaimsSender.ary_Video.count == 0 {
self.heightViewVideo.constant = 0.0
} else {
self.heightViewVideo.constant = 156.0
}
return self.dicMyClaimsSender.ary_Video.count
} else {
if self.dicMyClaimsSender.ary_Document.count == 0 {
self.heightViewFile.constant = 0.0
} else {
self.heightViewFile.constant = 156.0
}
return self.dicMyClaimsSender.ary_Document.count
}
} else {
if collectionView.tag == 2 {
if self.dicMyClaimsReceiver.ary_Images.count == 0 {
self.heightViewImg.constant = 0.0
} else {
self.heightViewImg.constant = 156.0
}
return self.dicMyClaimsReceiver.ary_Images.count
} else if collectionView.tag == 3 {
if self.dicMyClaimsReceiver.ary_Video.count == 0 {
self.heightViewVideo.constant = 0.0
} else {
self.heightViewVideo.constant = 156.0
}
return self.dicMyClaimsReceiver.ary_Video.count
} else {
if self.dicMyClaimsReceiver.ary_Document.count == 0 {
self.heightViewFile.constant = 0.0
} else {
self.heightViewFile.constant = 156.0
}
return self.dicMyClaimsReceiver.ary_Document.count
}
}
}