iPhone: используйте UITableViewCellStyleValue1 и UITableViewCellStyleValue2 в одном табличном представлении - PullRequest
0 голосов
/ 04 января 2011

Каков наилучший способ использовать оба UITableViewCellStyleValue1 и UITableViewCellStyleValue2 в одном и том же tableView? Я не уверен, что лучший способ сделать это, так как я initWithStyle до моего переключения:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

    }

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = @"Username";

                break;
            case 1: 
                cell.textLabel.text = @"Password";
                break;
        }//end switch

    } else if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0: 
                cell.textLabel.text = @"Create Account";

                break;
        }//end switch
    }

    return cell;

}//end

Как я могу использовать UITableViewCellStyleValue2 в моей ячейке "Создать учетную запись"?

Ответы [ 2 ]

1 голос
/ 04 января 2011

Определите стиль и идентификатор ячейки перед вызовом initWithStyle.

Вам необходимо использовать разные идентификаторы повторного использования ячеек для каждого стиля, потому что если ячейка со стилем Value1 будет выведена из очереди, вы не захотитеповторно использовать его для строки, которая нуждается в стиле Value2.

static NSString *CellIdentifierValue1 = @"CellIdentifierValue1";
static NSString *CellIdentifierValue2 = @"CellIdentifierValue2";

//default settings...
NSString *reuseIdentifier = CellIdentifierValue1;
UITableViewCellStyle cellStyle = UITableViewCellStyleValue1;

if ((indexPath.section == 1) && (indexPath.row == 0))
{
    //special settings for CreateAccount...
    reuseIdentifier = CellIdentifierValue2;
    cellStyle = UITableViewCellStyleValue2;
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:cellStyle reuseIdentifier:reuseIdentifier] autorelease];
}

//rest of existing code...
1 голос
/ 04 января 2011

Почему бы вам не переместить initWithStyle внутри коммутатора?Если вы пытаетесь сэкономить место и вам нужно редкое значение Value2 по сравнению с Value1, вы можете оставить initWithStyle перед переключением, а затем снова создать его внутри switch для ячеек стиля Value2.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...