私の-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法では、セルラベルのYouTubeビデオの日付を返すこのコードがあります:
//Video upload date
NSString *formattedString = [[[self.videos objectAtIndex:indexPath.row] objectForKey:@"uploaded"] stringByReplacingOccurrencesOfString:@"T" withString:@""];
formattedString = [formattedString stringByReplacingCharactersInRange:NSMakeRange(18, 5) withString:@""];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [df dateFromString:formattedString];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];
cell.date.text = dateStr;
私のメソッドの実装は少し乱雑に見え、正しいアプローチは何か疑問に思っています。このコードをそのままにしておくべきですか、それとも日付の書式設定用に別のプライベート メソッドを作成する必要がありますか?
プライベートメソッドを作りたいとしましょう。何かのようなもの:
-(NSString *)formatDateWithString:(NSString *) mystring {
NSString *formattedString = [mystring stringByReplacingOccurrencesOfString:@"T" withString:@""];
formattedString = [formattedString stringByReplacingCharactersInRange:NSMakeRange(18, 5) withString:@""];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [df dateFromString:formattedString];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];
return dateStr;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
メソッドに何を書くべきですか?
私のプライベート メソッドの引数は次のようになります[self.videos objectAtIndex:indexPath.row]
が、このメソッドをどのように呼び出すか、どのインスタンス変数で呼び出すかを失いました。
ビュー クラスでインスタンス変数を作成する必要がありNSString
ますか、それともメソッドで NSString インスタンスを開始し、その-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
上でプライベート メソッドを実行できますか?