2013-06-25 39 views
0

我有Java和Android開發經驗,現在正在嘗試學習Objective-C和iPhone/iPad開發。爲了幫助自我教導,我正在將我爲Android製作的應用程序重新寫入iPhone。從對象數組中設置UITextView單元格標籤

我想創建一個tableView,它將包含成員的名稱,然後按下時在不同的視圖中有聯繫人信息。具體來說,我無法從我的NSMutableArray「currentMembers」設置我的單元格標籤,該標籤充滿了具有五個屬性的NSObject「member」,即NSStrings。其中兩個變量包括名字和姓氏。我希望將單元格標籤設置爲全名,以便連接字符串firstName + lastName。以下是我的tableview類的一些代碼。

**只是爲了澄清,我的數組確實充滿了正確定義的對象,而不是零

.M

@implementation CurrentMemberViewController 

@synthesize currentMembers = _currentMembers; 
@synthesize alumniMembers = _alumniMembers; 
@synthesize currentMembersLoadCheck = _currentMembersLoadCheck; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    if(self.currentMembersLoadCheck == NO) 
    { 
     NSString *currentFilePath = [[NSBundle mainBundle] pathForResource:@"akpsi_contact_list"                ofType:@"txt"]; 
     // NSString *alumniFilePath = [[NSBundle mainBundle] pathForResource:@"akpsi_alumni_list"               ofType:@"txt"]; 

     [self readFileString:currentFilePath]; 
     //[self readFileString:alumniFilePath]; 

     self.currentMembersLoadCheck = YES; 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.currentMembers count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Current Member Name"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    //if you dont have string, make default cell 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.textLabel.text = [self.currentMembers objectAtIndex:indexPath.row]; 
    //rest of implementation for creating cell label.. above is not correct 

    return cell; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
} 

- (NSString *)loadFileToString:(NSString *)filePath 
{ 
    NSError *error = nil; 
    NSString *fileContent = [NSString stringWithContentsOfFile:filePath 
                 encoding:NSUTF8StringEncoding 
                 error:&error]; 
    if(error) 
    { 
     NSLog(@"ERROR while loading from file: %@", error); 
    } 
    return fileContent; 
} 

-(void)readFileString:(NSString *)filePath 
{ 
    NSScanner *scanner = [NSScanner scannerWithString: [self loadFileToString:filePath]]; 
    //temporary variables 
    NSString *thisFirstName; 
    NSString *thisLastName; 
    NSString *thisPhoneNum; 
    NSString *thisEmail; 
    NSString *thisPledge; 
    NSString *thisMajor; 

    NSMutableArray *memberArray = [[NSMutableArray alloc]initWithCapacity:40]; 

    //begin scanning string until end 
    while ([scanner isAtEnd] == NO) 
    { 
     //scan one line, set variables, begin at next line 
     NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; 
     [scanner scanUpToCharactersFromSet:whitespace intoString:&thisFirstName]; 
     [scanner scanCharactersFromSet:whitespace intoString:nil]; 
     [scanner scanUpToCharactersFromSet:whitespace intoString:&thisLastName]; 
     [scanner scanCharactersFromSet:whitespace intoString:nil]; 
     [scanner scanUpToCharactersFromSet:whitespace intoString:&thisPhoneNum]; 
     [scanner scanCharactersFromSet:whitespace intoString:nil]; 
     [scanner scanUpToCharactersFromSet:whitespace intoString:&thisEmail]; 
     [scanner scanCharactersFromSet:whitespace intoString:nil]; 
     [scanner scanUpToCharactersFromSet:whitespace intoString:&thisPledge]; 
     [scanner scanCharactersFromSet:whitespace intoString:nil]; 
     NSCharacterSet *newLineCharacterSet = [NSCharacterSet newlineCharacterSet]; 
     [scanner scanUpToCharactersFromSet:newLineCharacterSet intoString:&thisMajor]; 
     [scanner scanCharactersFromSet:newLineCharacterSet intoString:nil]; 

     //create member object 
     AKPsiMember *member = [[AKPsiMember alloc] init]; 
     member.firstName = thisFirstName; 
     member.lastName = thisLastName; 
     member.phoneNum = thisPhoneNum; 
     member.emailAddress = thisEmail; 
     member.pledgeClass = thisPledge; 
     member.major = thisMajor; 
     //add to array depending on file being read 
     //broken?// 
     [memberArray addObject:member]; 
    } 
    self.currentMembers = memberArray; 
} 


@end 
+0

你是什麼意思「這些」成員「對象由五個NSString變量組成。」?你的意思是你使用NSMutableDictionary和NSMutableDictionary? –

+0

對不起,我沒有澄清,我有一個NSMutableArray「currentMembers」,充滿NSObjects「成員」,有五個屬性,NSStrings。我會更新我的描述。 – bmjohns

回答

1

你在談論的邏輯應該是cellForRowAtIndexPath:和應是沿着線:

AKPsiMember *member = [self.currentMembers objectAtIndex:indexPath.row]; 
NSString *fullName = [NSString stringWithFormat:@"%@ %@", member.firstName, member.lastName]; 

cell.textLabel.text = fullName; 

如果名字或姓氏都不能是零以上不會產生一個漂亮的全名字符串...


objectAtIndex:與細胞無關。它是一個數組方法來獲取指定索引處的值。在這種情況下,索引被指定爲indexPath.row,它只是表格的當前行(作爲索引路徑提供,因爲可能還有相關部分)。

您似乎沒有使用任何原型單元格。你只是重複使用單元格實例。表視圖爲你管理這個,你需要做的就是在你自己創建一個實例之前,先從池中獲取一個實例(使用dequeueReusableCellWithIdentifier:)。

+0

問題解決了!來自android/java的一些知識,原型cell/cellForRowAtIndexPath對我來說真的很難纏繞我的頭。任何機會,你可以做我最後一個忙,並解釋objectAtIndex背後的邏輯:indexPath.row? – bmjohns

相關問題