我有Java和Android开发经验,现在正在尝试学习Objective-C和iPhone / iPad开发。为了帮助自学,我正在将我为Android制作的应用程序重新编写到iPhone上。
我正在尝试创建一个tableView,它将包含成员的名称,然后在按下时在不同的视图中显示联系人信息。具体来说,我无法从我的NSMutableArray“currentMembers”设置我的单元格标签,该标签填充了NSObjects“member”,它有五个属性,即NSStrings。其中两个变量包括名字和姓氏。我想将单元格标签设置为全名,因此是firstName + lastName的串联字符串。以下是我的tableview类中的一些代码。
**只是为了澄清,我的数组确实填充了正确定义但不是nil的对象
的.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 :(得分: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:
),然后再自己创建一个实例。