自定义Cell的用法

 1.新建->User Interface->empty命名为DefinedCharacterCell.xib
2.在xib中添加一个UITableView Cell,并在其中添加几个label;
3.在DefinedCharacterCell.xib中将File's Owner的class改成你要加载的那个视图类的名字,这里是LBQCharacterTableViewController
4.在LBQCharacterTableViewController.h中添加一条语句:
@property (nonamtic,retain)IBOutlet UITalbeViewCell *definedCharacterCell;
5.将新建的Cell中的控件连线
加载自定义Cell和初始化Cell代码如下: 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [[NSBundle mainBundle]loadNibNamed:@"DefinedCharacterCell" owner:self options:nil];
        cell = self.definedCharacterCell;
    }
    
    // Configure the cell...
    UILabel * subjectlable = (UILabel *)[cell viewWithTag:1];
    UILabel * descriptionlable = (UILabel *)[cell viewWithTag:2];
    UILabel * datelable = (UILabel *)[cell viewWithTag:3];
    UILabel * prioritylable = (UILabel *)[cell viewWithTag:4];

    //ToDo* aToDo = [self.characterToDoList todoAtIndex:indexPath.row];
    ToDo *aToDo = [self.characterArray objectAtIndex:indexPath.row];
    subjectlable.text = aToDo.subject;
    descriptionlable.text = aToDo.todoDescription;
    datelable.text = aToDo.date;
    prioritylable.text = [NSString stringWithFormat:@"%d",aToDo.priority];
    return cell;
}
//设置TableView中的Cell与自定义的Cell的高度相同 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

你可能感兴趣的:(ios学习)