問題描述
iPhone SDK:檢查 UITableView 中每個對象的 ID 號並相應地更改徽章號 (iPhone SDK: Checking the ID number for each object in a UITableView and change the badge number accordingly)
I'm tryin to use some code to create a badge number for the iPhone App Icon.
I can successfully do this, however the code I am using (below) is pretty bad and could easily be done better, however I do not have the logic knowledge to be able to do this.
This is what it's doing...
- Get's the 'ID' of a user from the first message in a UITableView. It then checks this against the stored ID (from the first object when it was last loaded) and makes the badge number 1.
- Then checks the second ID in a message to see if that is the same as the stored. If not it will make the badge number 2.
- etc etc..
What I want it to do is automatically check the IDs and add '1' to the badge number until the first post with the correct ID is found however, I'm not too sure on the coding.
Any help would be great!
Thanks
(The code is below:)
NSDictionary* tweetValues = [[NSDictionary alloc] init];
tweetValues = [tweets objectAtIndex:1];
NSString *newStatusID = [[tweetValues objectForKey:@"id"] stringValue];
NSString *oldStatusID = [[NSUserDefaults standardUserDefaults] stringForKey:@"latestID"];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
if([newStatusID isEqualToString:oldStatusID]){
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
} else {
tweetValues = [tweets objectAtIndex:2];
newStatusID = [[tweetValues objectForKey:@"id"] stringValue];
if ([newStatusID isEqualToString:oldStatusID]){
[UIApplication sharedApplication].applicationIconBadgeNumber = 1;
}else{
tweetValues = [tweets objectAtIndex:3];
newStatusID = [[tweetValues objectForKey:@"id"] stringValue];
if ([newStatusID isEqualToString:oldStatusID]){
[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
}else{
[UIApplication sharedApplication].applicationIconBadgeNumber = 3;
}
}
}
‑‑‑‑‑
參考解法
方法 1:
I'm not exactly sure what you are trying to, but looking at your code I think you need to use a for loop.
Also the first line of your code is leaks. I'm not sure what you are trying to do. As far as I can see you don't have to create an empty dictionary here.
The line where you retrieve your tweetValues (tweetValues = [tweets objectAtIndex:1];
) is also strange. You are retrieving the object at index 1. Since arrays are zero‑indexed you are retrieving the second object in the array. If you wanted to retrieve the first you need to call: [tweets objectAtIndex:0];
If I had to refactor your code I would probably do something like:
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
NSString *oldStatusID = [[NSUserDefaults standardUserDefaults] stringForKey:@"latestID"];
for (NSDictionary *tweetValues in tweets) { // Iterate over all the tweetValues in tweets (assume tweets exist?)
NSString *newStatusID = [[tweetValues objectForKey:@"id"] stringValue];
if ([oldStatusID isEqualToString:newStatusID]) {
[UIApplication sharedApplication].applicationIconBadgeNumber++; // Increase the badge number by 1
}
}
I hope I correctly interpreted your code. If not, some more context might be useful.
方法 2:
I ended up doing the follow which seems to be working:
NSDate *dateRefreshed = [[NSUserDefaults standardUserDefaults] objectForKey:@"dateRefreshed"];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"dateRefreshed"];
[[NSUserDefaults standardUserDefaults] synchronize];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
for (NSDictionary *tweetValues in tweets) { // Iterate over all the tweetValues in tweets (assume tweets exist?)
NSDate *dates = [tweetValues objectForKey:@"created_at"];
NSTimeInterval sincePostDate = [[NSDate date] timeIntervalSinceDate: dates];
NSTimeInterval sinceNow = [[NSDate date] timeIntervalSinceDate: dateRefreshed];
int timeDiff = sincePostDate;
int timeSinceNow = sinceNow;
if (timeDiff <= timeSinceNow){
[UIApplication sharedApplication].applicationIconBadgeNumber++; // Increase the badge number by 1
}
}
If anyone has any better ideas, please feel free to post the code :)
(by Domness、klaaspieter、Domness)