UITableView Putting array objects into sections
Hey guys I need to know who to put these array objects into two separate sections, that means 开发者_开发问答one section for the red cell and another section for the blue cell. Would really appreciate your help been stuck on this all day now. Here is my code:
-(void)viewDidLoad {
[super viewDidLoad];
//Initialize the array.
array = [[NSMutableArray alloc] init];
[array addObject:@"Red"];
[array addObject:@"Blue"];
self.ViewTable.backgroundColor = [UIColor clearColor];
}
You need to implement:
return number of sections:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return number of rows for the requested section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return correct cell by reading both indexPath.row and indexPath.section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
So for red you would look for a cell request that has indexPath.section equal to 0 and indexPath.row equal to 0. blue would be indexPath.section equal to 1 and indexPath.row equal to 0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2; // This returns 2 sections
}
Updated:
In the cellForRowAtIndexPath
NSInteger section = [IndexPath section];
if (section == 0)
// write your code for red here
if (section == 1)
// write your code for blue here
精彩评论