Compare 2 Results by object of selected index of filteredList swift4
I have 2 lists of same custom object i.e. News
class News: Object {
@objc dynamic var title: String?
@objc dynamic var date: Date?
@objc dynamic var contentUrl: String?
}
and 2 lists are
var filteredList: Results<News>! = nil
var newsList: Results<News> {
get {
return realm.objects(News.self).sorted(byKeyPath: "date", ascending:
false)
}
}
I am filtering the list and the filtered data is storing in filteredList
.
Now I want to send the IndexPath
of newsList
to the next VC. When I search in the list, filteredList is appending to the tableView which is absolutely correct. When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList
and should return that indexPath /index of newsList
I have done it by my own way
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
searchController.dismiss(animated: true, completion: nil)
var sendIndex : IndexPath?
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
if !searchBarIsEmpty(){
for (index,news) in newsList.enumerated(){
if news.title == filteredList[indexPath.row].title{
sendIndex = IndexPath(row: index, section: 0)
}
}
}else{
sendIndex = indexPath
}
if let index = sendIndex{
let vc = NewsDetailsVC(collectionViewLayout: layout)
vc.tappedIndex = index
vc.title = "News Details"
vc.isSelectNews = true
self.navigationController?.pushViewController(vc, animated: true)
}
}
but it can be more optimise.. Please suggest some logic of optimisation
arrays swift
add a comment |
I have 2 lists of same custom object i.e. News
class News: Object {
@objc dynamic var title: String?
@objc dynamic var date: Date?
@objc dynamic var contentUrl: String?
}
and 2 lists are
var filteredList: Results<News>! = nil
var newsList: Results<News> {
get {
return realm.objects(News.self).sorted(byKeyPath: "date", ascending:
false)
}
}
I am filtering the list and the filtered data is storing in filteredList
.
Now I want to send the IndexPath
of newsList
to the next VC. When I search in the list, filteredList is appending to the tableView which is absolutely correct. When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList
and should return that indexPath /index of newsList
I have done it by my own way
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
searchController.dismiss(animated: true, completion: nil)
var sendIndex : IndexPath?
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
if !searchBarIsEmpty(){
for (index,news) in newsList.enumerated(){
if news.title == filteredList[indexPath.row].title{
sendIndex = IndexPath(row: index, section: 0)
}
}
}else{
sendIndex = indexPath
}
if let index = sendIndex{
let vc = NewsDetailsVC(collectionViewLayout: layout)
vc.tappedIndex = index
vc.title = "News Details"
vc.isSelectNews = true
self.navigationController?.pushViewController(vc, animated: true)
}
}
but it can be more optimise.. Please suggest some logic of optimisation
arrays swift
And what is the question?
– matt
Jan 2 at 6:21
When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList and should return that indexPath /index of newsList
– Kiran S Kulkarni
Jan 2 at 6:30
Show us the code which gives you the index from the filtered list.
– Rakesha Shastri
Jan 2 at 6:33
add a comment |
I have 2 lists of same custom object i.e. News
class News: Object {
@objc dynamic var title: String?
@objc dynamic var date: Date?
@objc dynamic var contentUrl: String?
}
and 2 lists are
var filteredList: Results<News>! = nil
var newsList: Results<News> {
get {
return realm.objects(News.self).sorted(byKeyPath: "date", ascending:
false)
}
}
I am filtering the list and the filtered data is storing in filteredList
.
Now I want to send the IndexPath
of newsList
to the next VC. When I search in the list, filteredList is appending to the tableView which is absolutely correct. When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList
and should return that indexPath /index of newsList
I have done it by my own way
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
searchController.dismiss(animated: true, completion: nil)
var sendIndex : IndexPath?
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
if !searchBarIsEmpty(){
for (index,news) in newsList.enumerated(){
if news.title == filteredList[indexPath.row].title{
sendIndex = IndexPath(row: index, section: 0)
}
}
}else{
sendIndex = indexPath
}
if let index = sendIndex{
let vc = NewsDetailsVC(collectionViewLayout: layout)
vc.tappedIndex = index
vc.title = "News Details"
vc.isSelectNews = true
self.navigationController?.pushViewController(vc, animated: true)
}
}
but it can be more optimise.. Please suggest some logic of optimisation
arrays swift
I have 2 lists of same custom object i.e. News
class News: Object {
@objc dynamic var title: String?
@objc dynamic var date: Date?
@objc dynamic var contentUrl: String?
}
and 2 lists are
var filteredList: Results<News>! = nil
var newsList: Results<News> {
get {
return realm.objects(News.self).sorted(byKeyPath: "date", ascending:
false)
}
}
I am filtering the list and the filtered data is storing in filteredList
.
Now I want to send the IndexPath
of newsList
to the next VC. When I search in the list, filteredList is appending to the tableView which is absolutely correct. When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList
and should return that indexPath /index of newsList
I have done it by my own way
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
searchController.dismiss(animated: true, completion: nil)
var sendIndex : IndexPath?
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
if !searchBarIsEmpty(){
for (index,news) in newsList.enumerated(){
if news.title == filteredList[indexPath.row].title{
sendIndex = IndexPath(row: index, section: 0)
}
}
}else{
sendIndex = indexPath
}
if let index = sendIndex{
let vc = NewsDetailsVC(collectionViewLayout: layout)
vc.tappedIndex = index
vc.title = "News Details"
vc.isSelectNews = true
self.navigationController?.pushViewController(vc, animated: true)
}
}
but it can be more optimise.. Please suggest some logic of optimisation
arrays swift
arrays swift
edited Jan 2 at 8:48
Rakesha Shastri
7,41621635
7,41621635
asked Jan 2 at 6:17
Kiran S KulkarniKiran S Kulkarni
100211
100211
And what is the question?
– matt
Jan 2 at 6:21
When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList and should return that indexPath /index of newsList
– Kiran S Kulkarni
Jan 2 at 6:30
Show us the code which gives you the index from the filtered list.
– Rakesha Shastri
Jan 2 at 6:33
add a comment |
And what is the question?
– matt
Jan 2 at 6:21
When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList and should return that indexPath /index of newsList
– Kiran S Kulkarni
Jan 2 at 6:30
Show us the code which gives you the index from the filtered list.
– Rakesha Shastri
Jan 2 at 6:33
And what is the question?
– matt
Jan 2 at 6:21
And what is the question?
– matt
Jan 2 at 6:21
When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList and should return that indexPath /index of newsList
– Kiran S Kulkarni
Jan 2 at 6:30
When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList and should return that indexPath /index of newsList
– Kiran S Kulkarni
Jan 2 at 6:30
Show us the code which gives you the index from the filtered list.
– Rakesha Shastri
Jan 2 at 6:33
Show us the code which gives you the index from the filtered list.
– Rakesha Shastri
Jan 2 at 6:33
add a comment |
1 Answer
1
active
oldest
votes
Here's a one-liner for finding the index to be sent.
if !searchBarIsEmpty() {
sendIndex = IndexPath(row: newsList.firstIndex(where: { $0.title == filteredList[indexPath.row].title }), section: 0)
}
Note: title
is assumed to be the primary key. Compare with the appropriate primary key.
great.. its done thanx
– Kiran S Kulkarni
Jan 2 at 8:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002032%2fcompare-2-resultscustomobject-by-object-of-selected-index-of-filteredlist-swif%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's a one-liner for finding the index to be sent.
if !searchBarIsEmpty() {
sendIndex = IndexPath(row: newsList.firstIndex(where: { $0.title == filteredList[indexPath.row].title }), section: 0)
}
Note: title
is assumed to be the primary key. Compare with the appropriate primary key.
great.. its done thanx
– Kiran S Kulkarni
Jan 2 at 8:44
add a comment |
Here's a one-liner for finding the index to be sent.
if !searchBarIsEmpty() {
sendIndex = IndexPath(row: newsList.firstIndex(where: { $0.title == filteredList[indexPath.row].title }), section: 0)
}
Note: title
is assumed to be the primary key. Compare with the appropriate primary key.
great.. its done thanx
– Kiran S Kulkarni
Jan 2 at 8:44
add a comment |
Here's a one-liner for finding the index to be sent.
if !searchBarIsEmpty() {
sendIndex = IndexPath(row: newsList.firstIndex(where: { $0.title == filteredList[indexPath.row].title }), section: 0)
}
Note: title
is assumed to be the primary key. Compare with the appropriate primary key.
Here's a one-liner for finding the index to be sent.
if !searchBarIsEmpty() {
sendIndex = IndexPath(row: newsList.firstIndex(where: { $0.title == filteredList[indexPath.row].title }), section: 0)
}
Note: title
is assumed to be the primary key. Compare with the appropriate primary key.
answered Jan 2 at 6:43
Rakesha ShastriRakesha Shastri
7,41621635
7,41621635
great.. its done thanx
– Kiran S Kulkarni
Jan 2 at 8:44
add a comment |
great.. its done thanx
– Kiran S Kulkarni
Jan 2 at 8:44
great.. its done thanx
– Kiran S Kulkarni
Jan 2 at 8:44
great.. its done thanx
– Kiran S Kulkarni
Jan 2 at 8:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002032%2fcompare-2-resultscustomobject-by-object-of-selected-index-of-filteredlist-swif%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
And what is the question?
– matt
Jan 2 at 6:21
When I selecting row from filteredList it is giving index of filteredList. Now though user selects filteredList's index , it should compare with the object of newList and should return that indexPath /index of newsList
– Kiran S Kulkarni
Jan 2 at 6:30
Show us the code which gives you the index from the filtered list.
– Rakesha Shastri
Jan 2 at 6:33