Skip to content

Instantly share code, notes, and snippets.

@Morse-Code
Created February 8, 2017 21:10
Show Gist options
  • Save Morse-Code/5e1a1afd09575f4e80d100932ae8e15a to your computer and use it in GitHub Desktop.
Save Morse-Code/5e1a1afd09575f4e80d100932ae8e15a to your computer and use it in GitHub Desktop.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *const cellContactIdentifier = @"ContactInfoCell";
static NSString *const cellCompanyIdentifier = @"CompanyCell";
static NSString *const cellDeleteIdentifier = @"DeleteCell";
static NSString *const cellDatePickerIdentifier = @"DatePicker";
static NSString *const cellAddPhoneIdentifier = @"AddPhoneCell";
static NSString *const cellAddressIdentifier = @"AddressCell";
static NSString *const cellCommentIdentifier = @"CommentCell";
static NSString *const cellMobilePhoneIdentifier = @"MobilePhoneCell";
NSString *cellIdentifier;
UITableViewCell *cell;
NSInteger logicalRow = (self.mode == ContactDetailsModeNormal) ? [self physicalIndexPathToWhatEverRowsWeHaveDataFor:indexPath] : [self physicalIndexPathToLogicalIndex:indexPath];
if(indexPath.section == 3)
cellIdentifier = cellDeleteIdentifier;
else if(indexPath.section == 0 &&
logicalRow == kContactDetailsAccountCell)
cellIdentifier = cellCompanyIdentifier;
else if(indexPath.section == 1 &&
indexPath.row == 0)
// The date cell uses the Company Cell identifier
cellIdentifier = cellCompanyIdentifier;
else if(indexPath.section == 1 &&
indexPath.row == 1)
cellIdentifier = cellDatePickerIdentifier;
else if(indexPath.section == 0 &&
logicalRow == kContactDetailsAddressCell)
cellIdentifier = cellAddressIdentifier;
else if(indexPath.section == 0 &&
tableView.isEditing &&
self.contact.phones.count != 3 &&
logicalRow == kContactDetailsPhoneNumbersCell + self.contact.phones.count)
cellIdentifier = cellAddPhoneIdentifier;
else if(indexPath.section == 2)
cellIdentifier = cellCommentIdentifier;
else
cellIdentifier = cellContactIdentifier;
NSString *name;
NSString *value;
ContactDetailsCell cellType;
UIKeyboardType keyboardType;
UIReturnKeyType returnKeyType;
JGSurveyContactAddress *address = [self.contact.addresses firstObject];
if (!((indexPath.section == 1 && indexPath.row == 0) ||
(indexPath.section == 1 && indexPath.row == 1) ||
(indexPath.section == 2) ||
(indexPath.section == 0 && tableView.isEditing && self.contact.phones.count != 3 && indexPath.row == kContactDetailsPhoneNumbersCell + self.contact.phones.count))) {
switch(logicalRow) {
case kContactDetailsFirstNameCell:
name = @"First Name";
value = self.contact.firstName;
cellType = kContactDetailsFirstNameCell;
keyboardType = UIKeyboardTypeASCIICapable;
returnKeyType = UIReturnKeyNext;
break;
case kContactDetailsLastNameCell:
name = @"Last Name";
value = self.contact.lastName;
cellType = kContactDetailsLastNameCell;
keyboardType = UIKeyboardTypeASCIICapable;
returnKeyType = UIReturnKeyNext;
break;
case kContactDetailsAccountCell:
name = @"Account";
value = self.contact.accountName;
break;
case kContactDetailsTitleCell:
name = @"Title";
value = self.contact.title;
cellType = kContactDetailsTitleCell;
keyboardType = UIKeyboardTypeASCIICapable;
returnKeyType = UIReturnKeyNext;
break;
case kContactDetailsAddressCell:
value = address.address;
cellType = kContactDetailsAddressCell;
break;
case kContactDetailsWebsiteCell:
name = @"Website";
value = self.contact.website;
cellType = kContactDetailsWebsiteCell;
keyboardType = UIKeyboardTypeURL;
returnKeyType = UIReturnKeyNext;
break;
case kContactDetailsDepartmentCell:
name = @"Department";
value = self.contact.department;
cellType = kContactDetailsDepartmentCell;
keyboardType = UIKeyboardTypeASCIICapable;
returnKeyType = UIReturnKeyNext;
break;
case kContactDetailsManagerCell:
name = @"Manager";
value = self.contact.manager;
cellType = kContactDetailsManagerCell;
keyboardType = UIKeyboardTypeASCIICapable;
returnKeyType = UIReturnKeyNext;
break;
case kContactDetailsAssistantNameCell:
name = @"Asst. Name";
value = self.contact.assistantName;
cellType = kContactDetailsAssistantNameCell;
keyboardType = UIKeyboardTypeASCIICapable;
returnKeyType = UIReturnKeyNext;
break;
case kContactDetailsAssistantPhoneCell:
name = @"Asst. Phone";
value = [self.contact.assistantPhone displayPhoneNumberAsStringWithExtension:self.contact.assistantPhoneExt];
cellType = kContactDetailsAssistantPhoneCell;
keyboardType = UIKeyboardTypePhonePad;
returnKeyType = UIReturnKeyNext;
break;
case kContactDetailsAssistantEmailCell:
name = @"Asst. Email";
value = self.contact.assistantEmail;
cellType = kContactDetailsAssistantEmailCell;
keyboardType = UIKeyboardTypeEmailAddress;
returnKeyType = UIReturnKeyNext;
break;
case kContactDetailsEmailCell:
name = @"Email";
value = self.contact.email;
cellType = kContactDetailsEmailCell;
keyboardType = UIKeyboardTypeEmailAddress;
returnKeyType = UIReturnKeyNext;
break;
default: {
NSUInteger phoneIndex = (logicalRow - kContactDetailsPhoneNumbersCell);
JGSurveyContactPhone *phone = [self.contact.phones objectAtIndex:phoneIndex];
value = [phone.phoneNumber displayPhoneNumberAsStringWithExtension:phone.extension];
if (![tableView isEditing] && [phone.label isEqualToString:@"Mobile"] && [MFMessageComposeViewController canSendText]) {
cellIdentifier = cellMobilePhoneIdentifier;
}
name = phone.label;
cellType = kContactDetailsPhoneNumbersCell;
keyboardType = UIKeyboardTypePhonePad;
returnKeyType = UIReturnKeyNext;
} break;
}
}
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(indexPath.section == 3)
// Return the delete cell
return cell;
else if(indexPath.section == 1 &&
indexPath.row == 0) {
// Date cells
name = @"Birthday";
value = [self displayBirthdayAsString:self.contact.birthday];
cellType = kContactDetailsBirthdayCell;
keyboardType = UIKeyboardTypeASCIICapable;
returnKeyType = UIReturnKeyNext;
}
else if(indexPath.section == 1 &&
indexPath.row == 1) {
// Return the date picker cell
UIDatePicker *datePicker = (UIDatePicker *)[cell viewWithTag:kDatePickerTag];
// Don't allow dates past today
[datePicker setMaximumDate:[NSDate date]];
return cell;
}
else if(indexPath.section == 2) {
// Notes cell
name = @"Comment";
value = self.contact.note;
cellType = kContactDetailsNoteCell;
keyboardType = UIKeyboardTypeASCIICapable;
returnKeyType = UIReturnKeyDone;
}
else if(indexPath.section == 0 &&
tableView.isEditing &&
self.contact.phones.count != 3 &&
indexPath.row == kContactDetailsPhoneNumbersCell + self.contact.phones.count) {
// If we're beyond the length of the phone numbers, then it's the add cell.
cell.textLabel.textColor = self.view.tintColor;
return cell;
}
cell.textLabel.text = name;
if(cellIdentifier == cellContactIdentifier) {
JGContactInfoDetailTableViewCell *contactCell = (JGContactInfoDetailTableViewCell *)cell;
if(value.length) {
contactCell.detailTextField.text = value;
[contactCell.detailTextField sizeToFit];
}
else {
contactCell.detailTextField.text = nil;
}
contactCell.cellType = cellType;
contactCell.detailTextField.keyboardType = keyboardType;
contactCell.detailTextField.returnKeyType = returnKeyType;
if(![tableView isEditing] &&
!(cellType == kContactDetailsPhoneNumbersCell ||
cellType == kContactDetailsEmailCell))
contactCell.selectionStyle = UITableViewCellSelectionStyleNone;
// Don't autocorrect for anything except for the notes cell
contactCell.detailTextField.autocorrectionType = UITextAutocorrectionTypeNo;
// Reset warning for invalid data if we're rebuilding the cell
[contactCell.detailTextField setRequiresInput:NO];
if(cellType == kContactDetailsFirstNameCell ||
cellType == kContactDetailsLastNameCell ||
cellType == kContactDetailsTitleCell ||
cellType == kContactDetailsDepartmentCell) {
// Capitalize the first character
contactCell.detailTextField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
}
else if(cellType == kContactDetailsWebsiteCell ||
cellType == kContactDetailsManagerCell ||
cellType == kContactDetailsAssistantNameCell) {
// Capitalize each word
contactCell.detailTextField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
}
else
contactCell.detailTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
if((cellType == kContactDetailsEmailCell ||
cellType == kContactDetailsAssistantEmailCell) &&
value.length) {
contactCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"email"]];
contactCell.editingAccessoryType = UITableViewCellAccessoryNone;
}
else if((cellType == kContactDetailsPhoneNumbersCell ||
cellType == kContactDetailsAssistantPhoneCell) &&
value.length) {
contactCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"phone"]];
contactCell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else if(cellType == kContactDetailsAssistantPhoneCell) {
// Show the disclosure indicator even if we don't have a value
contactCell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else if(cellType == kContactDetailsWebsiteCell &&
value.length) {
contactCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"website"]];
contactCell.editingAccessoryType = UITableViewCellAccessoryNone;
}
else {
contactCell.accessoryView = nil;
contactCell.editingAccessoryType = UITableViewCellAccessoryNone;
}
}
else if(cellIdentifier == cellAddressIdentifier) {
JGAddressTwoLineTableViewCell *addressCell = (JGAddressTwoLineTableViewCell *)cell;
JGSurveyContactAddress *address = [self.contact.addresses firstObject];
if(address) {
NSMutableString *addressString1 = [NSMutableString new];
[addressString1 appendFormat:@"%@", address.address1 ?: @""];
if(address.address2.length)
[addressString1 appendFormat:@"\n%@", address.address2];
// Only add the newline if we had an address 1 or 2
if(address.address1.length ||
address.address2.length)
[addressString1 appendString:@"\n"];
if(address.city.length)
[addressString1 appendFormat:@"%@", address.city];
if(address.state.length) {
if(address.city.length)
[addressString1 appendString:@", "];
[addressString1 appendFormat:@"%@", address.state];
}
if(address.zipcode.length)
[addressString1 appendFormat:@" %@", [address.zipcode displayZipCodeAsString]];
addressCell.addressLine.text = addressString1;
addressCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"location"]];
}
else {
addressCell.addressLine.text = nil;
}
}
else if(cellIdentifier == cellCommentIdentifier) {
JGContactCommentTableViewCell *commentCell = (JGContactCommentTableViewCell *)cell;
if(value.length) {
commentCell.commentTextView.text = value;
[commentCell.commentTextView sizeToFit];
}
else {
commentCell.commentTextView.text = nil;
}
commentCell.commentTextView.keyboardType = keyboardType;
commentCell.commentTextView.returnKeyType = returnKeyType;
commentCell.commentTextView.autocapitalizationType = UITextAutocapitalizationTypeSentences;
commentCell.commentTextView.autocorrectionType = UITextAutocorrectionTypeYes;
currentTextView = commentCell.commentTextView;
}
else if (cellIdentifier == cellMobilePhoneIdentifier) {
JGContactTableMobilePhoneCell *mobilePhoneCell = (JGContactTableMobilePhoneCell *)cell;
if(value.length) {
mobilePhoneCell.detailTextField.text = value;
[mobilePhoneCell.detailTextField sizeToFit];
}
else {
mobilePhoneCell.detailTextField.text = nil;
}
mobilePhoneCell.cellType = cellType;
mobilePhoneCell.mobilePhoneDelegate = self;
if([self indexPathIsPhoneNumber:indexPath] &&
[[[self phoneAtIndexPath:indexPath] phoneNumber] length]) {
mobilePhoneCell.phoneNumber = [[self phoneAtIndexPath:indexPath] phoneNumber];
}
} else {
cell.detailTextLabel.text = value;
if(cellIdentifier == cellCompanyIdentifier &&
cellType == kContactDetailsBirthdayCell) {
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else if(cellIdentifier == cellCompanyIdentifier)
cell.accessoryType = (self.contact.account && [self isAllowedToTransitionToAccount:self.contact.account]) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
if(![tableView isEditing])
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[self configureDetailTextViewColorForCell:cell];
return cell;
}
@Morse-Code
Copy link
Author

Worst code ever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment