What is the proper way of hard-coding sections in a UITableView?
Posted
by Sheehan Alam
on Stack Overflow
See other posts from Stack Overflow
or by Sheehan Alam
Published on 2010-05-25T16:37:11Z
Indexed on
2010/05/25
16:41 UTC
Read the original article
Hit count: 199
I have a UITableView with 3 sections that are hard coded. Everything is working fine, but I am not sure if I am doing it correctly.
Define number of rows in section:
- (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows;
//Bio Section
if(section == 0){
rows = 2;
}
//Profile section
else if(section == 1){
rows = 5;
}
//Count section
else if(section == 2){
rows = 3;
}
}
return rows;
}
Here is where I build my cells:
- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tblView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.numberOfLines = 5;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:(10.0)];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
if ([self.message_source isEqualToString:@"default"]) {
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = [Utils formatMessage:[NSString stringWithFormat: @"%@", mySTUser.bio]];
cell.detailTextLabel.text = nil;
}
else if(indexPath.row == 1){
cell.textLabel.text = [NSString stringWithFormat: @"%@", mySTUser.website];
cell.detailTextLabel.text = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
} //more code exists, but you get the point...
Now I define my number of sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tblView
{
return 3;
}
Is this the proper way of hard-coding my UITableView? Will I run into any issues when cells are reused?
© Stack Overflow or respective owner