views:

218

answers:

5

I have a class named Person and in this class is the property PersonName (amongst others).

A MutableArray MyUserInfoArr contains many Person objects.

I want to list each PersonName in a Cell of a TableView? How do I do this? Thanks in advance.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = @"Cell";
    Person *myPerson;    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
       cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *cellValue = [myUserInfoArr objectAtIndex:indexPath.row];
    cell.text = cellValue;
+2  A: 

The easiest way would be to just set the text with:

Person *person = [MyUserInfoArr objectAtIndex:[indexPath indexAtPosition:1]];
cell.textLabel.text = person.PersonName;

indexPath contains two indexes, first is the section index, the 2nd is the item's index within the section. So I'm assuming in your case you have a single section, that's why there is nothing to do with the indexAtPosition:0.

You also have to set up the table's data source methods, these tell your table view how many sections/rows it should show:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [MyUserInfoArr count];
}
Prody
Thanks for your reply, it displayed the numbers of rows in my TableView but in the line:cell.textLabel.text = [MyUserInfoArr objectAtIndex:[indexPath indexAtPosition:1]].PersonNameThere's an error : Request more member 'PersonName' in something not a structure or union. What does it mean then?
ThyPhuong
@ThyPhuong: The error probably meant that the thing returned was not a Person*. I think the problem was that the chained syntax was not supported. I've edited my above answer, try now.
Prody
A: 

Thanks for your reply, it displayed the numbers of rows in my TableView but in the line:

cell.textLabel.text = [MyUserInfoArr objectAtIndex:[indexPath indexAtPosition:1]].PersonName

There's an error : Request more member 'PersonName' in something not a structure or union. What does it mean then?

ThyPhuong
objectAtIndex return type is id, so to access your object property you must cast it to your type like((MyUserInfo*)[MyUserInfoArr objectAtIndex:[indexPath indexAtPosition:1]]).PersonName
Vladimir
A: 

@Prody : Maybe you misunderstood what I meaned.

Mytableview has 1 section and [MyUserInfoArr count] rows

I want to get the value of the Person.Name in each object Person in the MutableArray MyUserInfoArr and set to each TableView.cell.text = 1 Person.Name. But still not done yet? Thanks anyway.

ThyPhuong
+1  A: 
Person *person = [myUserInfoArr objectAtIndex:indexPath.row];
cell.textLabel.text = person.PersonName;

The reason your code doesn't work: the text property needs to be an NSString *, not a Person *.

ianh
But [myUserInfoArr objectAtIndex:indexPath.row] in logic not return a NSString because myUserInfoArr contains many objects of Person Class.
ThyPhuong
A: 

if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; Person *myPerson = [myUserInfoArr objectAtIndex:indexPath.row]; cell.textLabel.text = myPerson.DisplayName; NSLog(@"abc %i",indexPath.section);

}

This is my code, it ran, but it just return the attribute Person.DisplayName of the last object Person in the myUserInfoArr. How to fix it?

ThyPhuong